Jorge L Williams wrote:
>
> Hey guys,
>
> 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");
> <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?
>
As an alternative to Marc's approach, here is one I like:
JaxbPerson chuck = null;
try {
chuck = r.get(JaxbPerson.class);
} catch (UniformInterfaceException e) {
ClientResponse resp = e.getResponse();
if (resp.getStatus() == 404) {
throw new NotFoundException(); // A RuntimeException class
defined in the client app
} else {
... deal with the error based on the response we actually
got ...
}
}
I'm working internally on a framework that would turn any such response
into one of a well-defined series of Java exceptions (like
NotFoundException for a 404) that the business logic pictured above
could throw, which means the client application developer would never
have to know anything about HTTP status codes in particular -- they just
handle exceptions in the typical manner for Java based apps.
Craig
>
> Thanks,
>
> jOrGe W.