由以下原因引起:java.lang.NoSuchMethodError:org.apache.http.conn.ssl.SSLConnectionSocketFactory

时间:2017-02-13 12:38:52

标签: spring-mvc spring-boot httpclient

我正在使用Spring Boot,Spring MVC和带有HttpClient的Spring Web。在@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 

任何帮助或建议表示赞赏

3 个答案:

答案 0 :(得分:0)

删除明确的httpcore依赖项(它将包含在httpclient中)并执行mvn clean install。如果问题仍然存在,请显示完整的POM,因为您可能安装了httpclient的冲突版本以及其他内容。

答案 1 :(得分:0)

我能够使用以下来源找到库冲突的来源:

”“很奇怪,也许是从另一个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。

原始来源:https://github.com/aws/aws-sdk-java/issues/938

答案 2 :(得分:0)

我曾经遇到过类似的问题。首先加载了旧的httpClient jar。更换该罐子后,它可以正常工作。

git-hours

error