2017-02-13 291 views
1

我使用Spring Boot,Spring MVC和Spring Web與HttpClient。雖然試圖創建的HttpClient對象@Configuration類我得到例外下面的代碼:引起:java.lang.NoSuchMethodError:org.apache.http.conn.ssl.SSLConnectionSocketFactory

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 

任何幫助或建議讚賞

回答

0

刪除明確的httpcore依賴項(它將包含在httpclient中)並執行mvn clean install。如果問題仍然存在,請顯示完整的POM,因爲您可能安裝了其他版本的httpclient衝突版本。

+0

謝謝你的回覆。我試過了,但現在我得到異常 '由於:java.lang.ClassNotFoundException:org.apache.http.ssl.TrustStrategy at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305) 在org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1157) ... 49 more' 我檢查了其他地方,他們建議使用' org.apache.httpcomponents 的HttpCore 4.4 '不工作 –

相關問題