我使用Spring Boot,Spring MVC和Spring Web与HttpClient。在尝试在@Configuration类中创建HttpClient对象时,我得到以下代码的异常:
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory)
.build();
POM依赖项
<properties>
<http.client>4.5.2</http.client>
<http.core>4.4.4</http.core>
</<properties>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${http.core}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${http.client}</version>
</dependency>
例外:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.http.client.HttpClient]: Factory method 'getHttpClient' thr
ew exception; nested exception is java.lang.NoSuchMethodError: org.apache.http.conn.ssl.SSLConnectionSocketFactory.<init>(Ljavax/net/ssl/SSLContext;Ljavax/net
/ssl/HostnameVerifier;)V
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
... 155 more
Caused by: java.lang.NoSuchMethodError: org.apache.http.conn.ssl.SSLConnectionSocketFactory.<init>(Ljavax/net/ssl/SSLContext;Ljavax/net/ssl/HostnameVerifier;)
V
at wsclient.RestClientConfig.getHttpClient(RestClientConfig.java:182)
at wsclient.RestClientConfig$$EnhancerBySpringCGLIB$$d3a9da1b.CGLIB$getHttpClient$2(<generated>)
at wsclient.RestClientConfig$$EnhancerBySpringCGLIB$$d3a9da1b$$FastClassBySpringCGLIB$$a56984ae.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:309)
at wsclient.RestClientConfig$$EnhancerBySpringCGLIB$$d3a9da1b.getHttpClient(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 156 more
任何帮助或建议表示赞赏
删除显式的httpcore
dependency(它将包含在httpclient
中)并执行mvn clean install
。如果问题仍然存在,请显示完整的POM,因为您可能安装了与其他东西安装的httpclient
冲突版本。
我能够使用此源找到库冲突的来源:
“这很奇怪,也许是从不同的jar加载Apache类。你能尝试运行以下内容并粘贴输出吗?它可能会给我们一个线索,它试图从这里获取这些类:org.apache.http.conn.ssl .SSLConnectionSocketFactory.class.getProtectionDomain()。getCodeSource()。的getLocation()。的getPath()”
一旦我找到了该位置,它原来是我的/.m2/repository/文件夹中的另一个jar文件,然后我清理了该文件夹,所以现在它引用了/org/apache/httpcomponents/httpclient/4.5.6/httpclient -4.5.6.jar jar。