Search code examples
javajersey-client

jersey client not throwing exceptions property


I'm using jersey client to post data to a webservice. I've noticed some weird behavior from Jersey client.

I'm using following code:

    WebResource resource=null;
    try {
        ClientResponse response =   resource.accept(mediaType).post(ClientResponse.class, requestEntity);

System.out.println("Successful response received, statusCode=" + jerseyClientResponse.getStatus());)

    } catch (UniformInterfaceException e) {
        ClientResponse r = e.getResponse();
        System.out.println("Exception from server, statusCode="+r.getStatus());
    }

If server returns 404 status, I'm expecting jersey to throw UniformInterfaceException exception but when I execute the code, I get following message:

Successful response received, statusCode=404

Can anyone tell me why is UniformInterfaceException not getting thrown?

I'm using jersey client version 1.18.

Thanks in advance!


Solution

  • Can anyone tell me why is UniformInterfaceException not getting thrown?

    It is defined in the contract for this method.

    UniformInterfaceException - if the status of the HTTP response is greater than or equal to 300 and c is not the type ClientResponse.

    Use a type other than client response to get the desired result.

        Client client = Client.create();
        WebResource webResource = client
                .resource("http://google.com/fake/no_url");
        Object requestEntity = null;
        final String post = webResource.post(String.class, requestEntity);
    

    Exception in thread "main" com.sun.jersey.api.client.UniformInterfaceException: POST http://google.com/fake/no_url returned a response status of 411 Length Required
        at com.sun.jersey.api.client.WebResource.handle(WebResource.java:686)