users@jersey.java.net

Re: [Jersey] Client API: How do you handle faults?

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Thu, 16 Oct 2008 17:28:30 -0400

On Oct 16, 2008, at 4:59 PM, Jorge L Williams wrote:
>
> I'm thinking of using the Client API, but before I do I have a
> question regarding how expected faults are handled.
>
> From looking at the JavaDoc it seems that I can do something like
> this...
>
> Client c = Client.create();
>
> WebResource r = c.resource("http://host/people/chuckNorris");
> JaxbPerson chuck = r.get(JaxbPerson.class);
>
> ...
>
> If chuck exists everything is okay, but if chuck doesn't exist the
> Jax-RS service returns a status code of 404 and an object of type
> JaxbFault. What's the best way of handling this in the Client API?
>
You can do something like this:

WebResource r = c.resource("http://host/people/chuckNorris");
ClientResponse cr = r.get(ClientResponse.class);
if (cr.getStatus() != Response.Status.OK.getStatusCode()) {
    JaxbFault f = cr.getEntity(JaxbFault.class);
    // do something to handle the error
} else {
    // get the entity from the response
    JaxbPerson chuck = cr.getEntity(JaxbPerson.class);
}

ClientResponse is a client API thing BTW.

Marc.

---
Marc Hadley <marc.hadley at sun.com>
CTO Office, Sun Microsystems.