简体   繁体   中英

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!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM