HttpClientBuilder - java.lang.NoSuchFieldError:INSTANCE

时间:2015-06-20 00:51:44

标签: java maven http

我有一个使用HttpClient执行HTTP请求的Maven Java项目。在我的本地Java Web Server上,一切正常。但在我将其部署到SAP Hana Cloud Platform后,我收到以下错误:

java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:52)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:56)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<clinit>(DefaultHttpRequestWriterFactory.java:46)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:72)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:84)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<clinit>(ManagedHttpClientConnectionFactory.java:59)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$InternalConnectionFactory.<init>(PoolingHttpClientConnectionManager.java:493)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:149)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:138)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:114)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:726)
at com.sap.hana.cloud.odata.service.OlingoSampleApp.getHttpclient(OlingoSampleApp.java:382)
at com.sap.hana.cloud.odata.service.OlingoSampleApp.getCsrfToken(OlingoSampleApp.java:374)
at com.sap.hana.cloud.odata.service.ODataCalls.doGet(ODataCalls.java:163)
...

我的依赖关系如下:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.3.5</version>
    <scope>compile</scope>
</dependency>

根据这个问题java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE from Mashape Unirest in Java application我尝试使用以下代码来获取ClassLoader Ressource。

ClassLoader classLoader = this.getClass().getClassLoader();
URL resource = classLoader.getResource("org/apache/http/impl/client/HttpClientBuilder.class");
return resource;

并返回以下内容

"jar:file:/some/path/WEB-INF/lib/httpclient-4.3.5.jar!/org/apache/http/impl/client/HttpClientBuilder.class"

您可以看到jar版本是相同的。我看到的每个类似的问题都有不连贯的jar版本作为失败的根源。这个错误还有其他原因吗?

更新

在评论中讨论后,我将更新我的问题:

依赖树:

enter image description here

所以现在我可以看到版本冲突,我删除了neo-java-web-api。 classLoader现在再次返回版本4.3.2。但我仍然从一开始就得到错误。

现在,没有neo-java-web-api的新的完整依赖关系树如下所示: enter image description here

1 个答案:

答案 0 :(得分:2)

我仍然无法找到实际问题的解决方案。但至少我可以找到一个解决方法。我将范围更改为<scope>provided</scope>并仅使用版本4.1.4中提供的函数。例如new DefaultHttpClient(cm);而不是HttpClientBuilder.create().build();