users@jersey.java.net

Re: [Jersey] GET rest service throwing exceptions

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 23 Jun 2009 10:13:56 +0200

On Jun 23, 2009, at 2:55 AM, Saavedra, Gisella wrote:

>
> I have the following resource method:
>
> @GET
> @Produces({"application/xml"})
> public Logs getAl() {
>
> // Verify parameters
> NodeConfig nodeConfig = ClusterNodeUtils.getNodeInfo();
>
> // Verify that current node directory path exists
> if (!
> FileCompressionUtils.dirExists(nodeConfig.getNodecfgLogDirPath())) {
> throw new
> RestApplicationException(Response.Status.PRECONDITION_FAILED, "Log
> directory does not exist");
> }
> …
> }
>
> At the client side, it is invoked as:
> try {
> String xmlResponse =
> res.accept(MediaType.APPLICATION_XML_TYPE).get(String.class);
> } catch (UniformInterfaceException e) {
> // log it
> }
>
> /////////////////////////
>
> The RestApplicationException class code is:
> public class RestApplicationException extends
> WebApplicationException {
> public RestApplicationException (Response.Status inStatus,
> String inMessage) {
> super(Response.status(inStatus).entity(inMessage).type("text/
> plain").build());
>
> }
>
>
> ////////////////////
>
> But at the client side, I do not get the exception thrown above. I
> get the String xmlResponse = “"Log directory does not exist"
>

You should get UniformInterfaceException thrown on the client side
because the response status code is >= 300.

Could you add logging to your client so you can observer the responses
being received from the server to verify that the precondition status
code, 412, was being returned:

   res.addFilter(new LoggingFilter());


> What do I need to do, either client or server side, to receive the
> exception I threw as an exception?
>

The Jersey client API is not a RPC-type system where Java classes that
are exceptions are propagated from server to client. The exception you
threw on the server side will not be reproduced on the client side.
Your RestApplicationException is defining an *HTTP response* that will
be sent to the client and not an exception that will be propagated.

Paul.

> Thanks.
>
>
>
>
> Gisella Saavedra
> Sr. Software Engineer
> gsaavedra_at_zebra.com
> <image001.gif>
> 1000 Broadway, Suite 150, Oakland, CA 94607 | T+1 510 267 5123 T
> Main+1 510 267 5000 F+1 510 267 5100 | http://www.zebra.com/zes
>
>
>