【问题标题】:caused by: java.lang.NoSuchMethodError: org.apache.http.conn.ssl.SSLConnectionSocketFactory引起:java.lang.NoSuchMethodError: org.apache.http.conn.ssl.SSLConnectionSocketFactory
【发布时间】:2017-07-01 10:48:49
【问题描述】:

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

任何帮助或建议表示赞赏

【问题讨论】:

    标签: java spring-boot spring-mvc httpclient


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复。我试过了,但现在我得到了异常Caused by: java.lang.ClassNotFoundException: org.apache.http.ssl.TrustStrategy at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1157) ... 49 more我检查了其他地方,他们建议使用&lt;dependency&gt; &lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt; &lt;artifactId&gt;httpcore&lt;/artifactId&gt; &lt;version&gt;4.4&lt;/version&gt; &lt;/dependency&gt;不工作
    【解决方案2】:

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

    “这很奇怪,也许它正在从不同的 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

    【讨论】:

      【解决方案3】:

      我也遇到过类似的问题。首先加载了一个旧的 httpClient jar。更换那个罐子后,它工作正常。

      java.lang.NoSuchMethodError: org.apache.http.conn.ssl.SSLConnectionSocketFactory.(Ljavax/net/ssl/SSLContext;Ljavax/net/ssl/HostnameVerifier;)V
      

      【讨论】:

        猜你喜欢
        • 2014-12-02
        • 2020-05-30
        • 2016-01-19
        • 2021-09-17
        • 2018-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-24
        相关资源
        最近更新 更多