users@jersey.java.net

Re: [Jersey] User defined exceptions

From: Ricardo Borillo <borillo_at_si.uji.es>
Date: Fri, 17 Jul 2009 07:50:13 +0200

Hi,

When i start designing server side, i review some well known REST APIs
implementations like Flickr or Twitter.

In Flickr API, an HTTP 200 error code is always returned:

http://www.flickr.com/services/api/flickr.photos.getInfo.html

My question is only about of what are the best practices for handle
normal responses and user defined exceptions in the client, using
Jersey and JAXB :)

Returning an HTTP 500 error code with the error information in the
messagebody, works perfect with the client API. But, This is the
proper way to implement it?

Thanks for your help

---
Salut,
====================================
Ricardo Borillo Domenech
http://xml-utils.com
On Thu, Jul 16, 2009 at 23:14, tarjei<tarjei_at_nu.no> wrote:
> 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>
>