0
votes

I want to test a simple REST call in post using jersey. I call this method from the main:

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();
    }
 }

Any suggestions? Did I forget something?

Here is an error I'm getting:

com.sun.jersey.api.client.ClientHandlerException: java.lang.NullPointerException at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:128) at com.sun.jersey.api.client.Client.handle(Client.java:457) at com.sun.jersey.api.client.WebResource.handle(WebResource.java:557) at com.sun.jersey.api.client.WebResource.access$300(WebResource.java:69) at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:499) at it.inail.util.RestCall.getEsearchOutputJson(RestCall.java:30) at it.inail.util.EntSearch.main(EntSearch.java:254) Caused by: java.lang.NullPointerException at com.sun.jersey.api.client.CommittingOutputStream.close(CommittingOutputStream.java:104) at com.sun.jersey.api.client.TerminatingClientHandler.writeRequestEntity(TerminatingClientHandler.java:294) at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:179) at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:126) ... 6 more

1
You call it, right, then what happen ? - Jean-François Savard
What do you mean "to test"? To test automatically? Maybe there are some constraints on consumed JSON - you should check then that if you try to POST invalid request you get correct error response. Also, if processing request persists something on the server, try to GET it back and check if it corresponds to your expectations. If it just processes request body - then response also should meet your expectations. - iTollu
i'm sorry, i've added the exception.. i want to receive an json output and print it.. i've tryed with httprequester extension from firefox and with that link i got response - robz S

1 Answers

0
votes
        //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);