Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法在spring引导中自动连接resttemplate_Java_Spring Boot_Javabeans_Autowired_Resttemplate - Fatal编程技术网

Java 无法在spring引导中自动连接resttemplate

Java 无法在spring引导中自动连接resttemplate,java,spring-boot,javabeans,autowired,resttemplate,Java,Spring Boot,Javabeans,Autowired,Resttemplate,这是我的配置类 @Component public class Teamplate { @Bean(name="restProxy") public RestTemplate restProxy() { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); String proxyHost = "10.123.10";

这是我的配置类

@Component
public class Teamplate {
    @Bean(name="restProxy")
    public RestTemplate restProxy() {

        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
            String proxyHost = "10.123.10";
            String proxyPort = "8080";
            String proxyUser = "test";
            String proxyPassword = "test";
            Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyHost, Integer.parseInt(proxyPort)));
            Authenticator.setDefault(new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    if (getRequestorType().equals(RequestorType.PROXY)) {
                        return new PasswordAuthentication(proxyUser, proxyPassword.toCharArray());
                    }
                    return super.getPasswordAuthentication();
                }
            });
        requestFactory.setProxy(proxy);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        return restTemplate;
    }
}
这是我的主课

@Component
public class Call {
    @Autowired
    @Qualifier("restProxy")
    private RestTemplate restProxy;

    public void send() {
        // some code
        restProxy.exchange(requestEntity, responseType)
        //
    }
}
这是我的进口货

import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.Proxy.Type;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
这是我对resttemplate的pom

<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.6</version>
        </dependency>

org.apache.httpcomponents
httpclient
4.5.6
当我运行应用程序时,它说 com.x.x.Call中的字段restProxy需要一个类型为“org.springframework.web.client.restemplate”的bean,但找不到该bean


我无法自动连接我的resttemplate,我还尝试将其设置为@primary,并使用@Qualifier(value=“restProxy”)

标记您需要使用
@Configuration
而不是
@Component
@123它不管用快速问题,您在哪里使用
org.apache.httpcomponents.httpclient
,我看到您正在使用
Spring的
restemplate
来执行APIcall@Avi我在添加超时时正在使用它,目前未使用它。不必要的依赖性我无法在本地重现此问题,您能否显示
主类
如果您在
组件扫描中忽略了,此问题可能会出现。您需要使用
@Configuration
而不是
@Component
@123这不是一个快速问题,您在哪里使用
org.apache.httpcomponents.httpclient
,我看到您正在使用
Spring的
restemplate
来执行APIcall@Avi我在添加超时时正在使用它,目前未使用它。不必要的依赖性我无法在我的本地计算机中重现该问题,您能否显示
主类
。如果您在
组件扫描中忽略了该问题,则可能会出现此问题