users@jersey.java.net

Re: [Jersey] User defined exceptions

From: tarjei <tarjei_at_nu.no>
Date: Thu, 16 Jul 2009 23:14:12 +0200

On 07/16/2009 09:33 PM, Craig McClanahan wrote:
> Ricardo Borillo wrote:
>> Hi all,
>>
>> I'm implementing my first REST API and i'm facing some design problems
>> ...
>>
>> In my server REST service i issue, through jaxb binding, an instance
>> of a class in application/xml format.
>> If an user defined exception arises, i issue an error message (i'm
>> using an exception mapper) with an HTTP 200 error code:
If you control the server side of the API, then you should try to answer
with a HTTP 500 containing the error information in the responsebody -
thus informing the client that there is an error on the other side.

I'm not sure how well that works with the client API , but IMHO that is
a cleaner option.

Regards,
Tarjei
>>
>> <error code="1" />
>>
>> This error message is built with JAXB too.
>>
>> The question is how to manage this situation in the client. If i have:
>>
>> ClientResponse response = webResource.path("test/x").accept(
>> MediaType.APPLICATION_XML).get(ClientResponse.class);
>>
>> if (response.getStatus() == 200)
>> {
>> return response.getEntity(MyJAXBEntity.class);
>> }
>> else
>> {
>> throw new Exception(response.getStatus());
>> }
>>
>> How can i know that i'm getting an error xml or other message using
>> JAXB binding?
>> How can i design this type of interaction?
>>
>> Thanks a lot and sorry if this is an obvious question :)
>>
> Would something like this work?
>
> ClientResponse response = webResource.path("test/x").accept(
> MediaType.APPLICATION_XML).get(ClientResponse.class);
>
> if (response.getStatus() == 200)
> {
> return response.getEntity(MyJAXBEntity.class);
> }
> else
> {
> err = response.getEntity(MyErrorMessage.class);
> throw new Exception(err.getCode());
> }
>
>
> where the MyErrorMessage class has a "code" property containing a string
> representation of the "code" value.
>
> Craig
>
>> ---
>> Salut,
>> ====================================
>> Ricardo Borillo Domenech
>> http://xml-utils.com
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>


-- 
Tarjei Huse
Mobil: 920 63 413