我想在post中使用jersey测试一个简单的REST调用。 我从主要方法中称这种方法:
public void TEST(JsonObject inputJson){
try {
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource webResource = client.resource("http://validate.jsontest.com");
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)
.post(ClientResponse.class, inputJson.toString());
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
String stringOutput = response.getEntity(String.class);
System.out.println(stringOutput);
} catch (Exception e) {
e.printStackTrace();
}
}
有什么建议吗? 我忘了什么吗?
这是我遇到的错误:
com.sun.jersey.api.client.ClientHandlerException: java.lang.NullPointerException at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:128) 在com.sun.jersey.api.client.Client.handle(Client.java:457)at com.sun.jersey.api.client.WebResource.handle(WebResource.java:557)at at com.sun.jersey.api.client.WebResource.access $ 300(WebResource.java:69) 在 com.sun.jersey.api.client.WebResource $ Builder.post(WebResource.java:499) at it.inail.util.RestCall.getEsearchOutputJson(RestCall.java:30)at at it.inail.util.EntSearch.main(EntSearch.java:254)引起: java.lang.NullPointerException at com.sun.jersey.api.client.CommittingOutputStream.close(CommittingOutputStream.java:104) 在 com.sun.jersey.api.client.TerminatingClientHandler.writeRequestEntity(TerminatingClientHandler.java:294) 在 com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:179) 在 com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:126) ......还有6个
答案 0 :(得分:0)
//orig
DefaultClientConfig config = new DefaultClientConfig();
config.getProperties().put(URLConnectionClientHandler.PROPERTY_HTTP_URL_CONNECTION_SET_METHOD_WORKAROUND, true);
Client c = Client.create(config);
WebResource r = c.resource("http://localhost:111/rd/capture");
r.method("POST",String.class,data);