users@jersey.java.net

Re: [Jersey] Re: Returning response in JSON format in case of Exceptions

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 10 May 2010 16:36:32 +0200

On May 10, 2010, at 4:19 PM, j2eeuser32 wrote:

>
> Paul,
>
> Great, thank you. That helps a little bit.
>
> My client is not Java based, that makes it difficult to catch the
> UniformInterfaceException.
>

But the program you are writing in Java presumably can?


> My intention is this:
> Client should never see any exceptions and should not deal with
> getting the
> entityText from the ClientResponse.
> It should receive all types of responses (successful or errors/
> exceptions)
> only in the String type (JSON formatted).
>
> The client can have a logic to look for { "error" : { "messageKey" :
> 123,
> "errorMessage" : "Invalid parameter ..." } } to see if it is an error
> message. Otherwise, it is successful message.
>
> I guess I have the following option:
>
> 1. To return a response status of less than 400 so that it wont
> throw an
> UniformInterfaceException
>

I would not recommending dong that, it is topsy turvy :-). The whole
point of status codes is the client can react differently depending on
the values.


> My only concern is whether I am handling all types of exceptions to
> make
> sure the response is always a String.
>

In this case if it is easier i recommend using ClientResponse and then
using getEntity which will work independent of the status code.

    ClientResponse clientResponse =
resource.path("/test/
dummy
").accept(MediaType.APPLICATION_JSON_TYPE).get(ClientResponse.class);
   clientResponse.getEntity(String.class));

Paul.