我正在尝试使用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)
答案 0 :(得分:7)
我遇到了上述错误,因为我的项目中有多个旧版本的HTTPClient。我删除了旧版本的HTTPClient,现在错误信息消失了。
答案 1 :(得分:2)
通过分析您的堆栈跟踪,我发现您的程序写得很好。似乎apache HTTP API编译错误。也许包org.apache.http.impl.client
是根据您使用的org.apache.http.conn.ssl
的不同版本编译的?将两个软件包升级到最新版本并重试。将其报告给HTTPClient bug跟踪器。
答案 2 :(得分:0)
我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)一起进行修复。