我正在尝试使用 HTTPClient 编写一个简单的 WebService 测试。我已加载所有罐子,但收到上述错误消息。
为什么我会收到此错误消息?
这是代码:
package HTTPClientTest;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.Assert;
public class RESTTester {
public static void main (String args[]) {
String restURL_XML = "http://parabank.parasoft.com/parabank/services/bank/customers/12212/";
try {
testStatusCode(restURL_XML);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void testStatusCode(String restURL) throws ClientProtocolException, IOException {
HttpUriRequest request = new HttpGet(restURL);
HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
Assert.assertEquals(httpResponse.getStatusLine().getStatusCode(),HttpStatus.SC_OK);
}
}
下面是错误堆栈跟踪,
Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:966)
at HTTPClientTest.RESTTester.testStatusCode(RESTTester.java:37)
at HTTPClientTest.RESTTester.main(RESTTester.java:22)
我出现上述错误是因为我的项目中有多个旧版本的 HTTPClient。我删除了旧版本的 HTTPClient,现在错误消息消失了。
通过分析你的堆栈跟踪,我发现你的程序写得很好。看来 apache HTTP API 编译错误。也许包
org.apache.http.impl.client
是针对与您正在使用的不同版本的 org.apache.http.conn.ssl
编译的?将两个软件包升级到最新版本并重试。向 HTTPClient 错误跟踪器报告。
我也有同样的问题。我在
apache-httpcomponents-httpcore.jar
和 httpclient-4.4.1.jar
旁边发现了一个名为 httpcore-4.4.1.jar
的罐子。
我只是删除了
apache-httpcomponents-httpcore.jar
并且工作得很好。
I
ve come across this error(java.lang.NoSuchFieldError: INSTANCE) several times. With different reasons here it is INSTANCE.
This error is conveying about the mismatch between the aws-java-sdk-core library and one of the service-specific SDK
aws-sdk-s3 或 aws-java-sdk-sqs。
我在将 aws-java-sdk-core(1.11.289) 与 aws-sdk-s3(1.11.428) 一起使用时遇到此错误。通过将以下 aws-java-sdk-core(1.11.289) 与 aws-sdk-s3(1.11.186) 结合使用来修复此问题。