我有一个简单的Spring应用程序,它有一个简单的REST客户机,如下所示:
@Service
public class MyRestClient {
private static final String url = "http://localhost:8080/";
private RestTemplate restTemplate;
@Autowired
public MyRestClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String invoke() {
return restTemplate.getForObject(url, String.class);
}
}这对Spring非常有效。
现在,我正在尝试将Spring Cloud添加到项目中,以实现丝带客户端负载平衡。我跟踪了这里的链接:
https://spring.io/guides/gs/client-side-load-balancing/
或者这里,它似乎是复制和粘贴,但具有更多更新的依赖关系:
http://www.baeldung.com/spring-cloud-rest-client-with-netflix-ribbon
即使不向MyRestClient添加任何注释,我也会添加以下内容:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>我得到以下例外:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.client.RestTemplate]: Factory method 'restTemplate' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/http/impl/client/HttpClients
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
... 31 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/apache/http/impl/client/HttpClients
at org.springframework.http.client.HttpComponentsClientHttpRequestFactory.<init>(HttpComponentsClientHttpRequestFactory.java:88) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_131]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_131]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_131]
at java.lang.Class.newInstance(Class.java:442) ~[na:1.8.0_131]
at org.springframework.beans.BeanUtils.instantiate(BeanUtils.java:77) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.boot.web.client.RestTemplateBuilder.detectRequestFactory(RestTemplateBuilder.java:596) ~[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.web.client.RestTemplateBuilder.configureRequestFactory(RestTemplateBuilder.java:559) ~[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.web.client.RestTemplateBuilder.configure(RestTemplateBuilder.java:527) ~[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.web.client.RestTemplateBuilder.build(RestTemplateBuilder.java:515) ~[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
at org.springframework.boot.web.client.RestTemplateBuilder.build(RestTemplateBuilder.java:501) ~[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]为什么我的REST客户端没有这个依赖项,但又没有添加任何注释或任何东西,当我添加这个依赖项时,我就得到了这个异常?
我尝试从文档或示例中添加各种依赖项,比如spring-cloud-dependencies (似乎不推荐)、spring-cloud-netflix等等,但都没有效果。
什么是正确的依赖添加,以使其发挥作用?
发布于 2017-09-23 19:46:37
它看起来像是这个论坛帖子中的问题,HttpClients所需要的依赖是org.apache.httpcomponents/httpclient,至少有4.3.3版本。
发布于 2017-09-26 22:09:52
您的带状依赖项通过ribbon-httpclient 2.2.2管理以下工件,直至4.5.3版本
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcomponents-client</artifactId>
<version>4.5.3</version>我建议为httpcomponents-client添加显式依赖,并将其排除在所有其他依赖项之外。
换句话说,尝试在编译中添加它。不是运行时:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>https://stackoverflow.com/questions/46270197
复制相似问题