You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Bill Smith <ne...@weseewhathappens.com> on 2013/06/27 18:38:21 UTC

WebClient dealing with errors

I have a Restservice that I am calling,

For Good responses it returns a JSON serialized Customer

when there is an error the service returns a JSON serialized ServiceError.

I need to get some details out of the ServiceError along with the
ResponseCode but am having some difficulties.

I call the service as follows

localClient.type(MediaType.APPLICATION_JSON);

Customer customer =  localClient.path("/customer/" + customerName

                .header("ContentType", "application/json")

                .get(Customer.class);


When an http 500 occurs I would like to get the ServiceError.  Any
suggestions on how to do this?

Re: WebClient dealing with errors

Posted by Sergey Beryozkin <sb...@gmail.com>.
On 27/06/13 17:38, Bill Smith wrote:
> I have a Restservice that I am calling,
>
> For Good responses it returns a JSON serialized Customer
>
> when there is an error the service returns a JSON serialized ServiceError.
>
> I need to get some details out of the ServiceError along with the
> ResponseCode but am having some difficulties.
>
> I call the service as follows
>
> localClient.type(MediaType.APPLICATION_JSON);
>
> Customer customer =  localClient.path("/customer/" + customerName
>
>                  .header("ContentType", "application/json")
>
>                  .get(Customer.class);
>
>
> When an http 500 occurs I would like to get the ServiceError.  Any
> suggestions on how to do this?
>
Starting from CXF 2.7.x:

catch (WebApplicationException ex) - this will catch all server errors

or

catch (InternalServerErrorException ex) {
}
catch (BadRequestException ex) {
}
catch (WebApplicationException ex) {
  // all others...
}

see "Exceptions" at

https://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/package-frame.html

Or you can do

Response r = wc.get();
if (r.getStatus() == 200) {
customer = response.readEntity(Customer.class);
} else {
// deal with the status error
}

Sergey