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我在添加超时时正在使用它,目前未使用它。不必要的依赖性我无法在我的本地计算机中重现该问题,您能否显示主类
。如果您在组件扫描中忽略了该问题,则可能会出现此问题