users@jersey.java.net

Re: [Jersey] Null semantics

From: Julio Faerman <jfaerman_at_gmail.com>
Date: Wed, 17 Dec 2008 10:15:50 -0200

Issue logged

https://jersey.dev.java.net/issues/show_bug.cgi?id=167

I'll try the exception mapper, thanks

On Wed, Dec 17, 2008 at 8:37 AM, Paul Sandoz <Paul.Sandoz_at_sun.com> wrote:
> Hi Julio,
>
> On Dec 16, 2008, at 8:28 PM, Julio Faerman wrote:
>
>> Hello,
>>
>> I need some help understanding the handling of nulls by jersey.
>>
>> On the server resource method, i try to get a bean from the database
>> by id and return it to be JAXB marshalled. When the bean does not
>> exist, i return null, generating a 204 response.
>> On jersey-client, a NullPointerException is thrown (trace below).
>>
>> Question 1: Is this correct or i should throw an
>> WebApplicationException with status 404 when the bean is not found? It
>> sounds wrong having to throw and exception in a non-exceptional case.
>>
>
> The returning of a 404 is the correct approach in this respect. Because you
> are informing the client that the resource does not exist.
>
> I guess it depends on what you mean by exceptional :-) in this case i would
> consider a 404 an exception to the normal processing of when something
> exists. Throwing exceptions in this case are very useful so that your flow
> of code can handle the non-exceptional common case.
>
> You could return a response like:
>
> return Response.status(404).build();
>
> but that will change the flow of your code. I would recommend throwing
> WebApplicationException or even better using an ExceptionMapper.
>
>
>
>> Question 2: Shouldn't i get a null from WebResource.get()? is it
>> possible or it is a exception i must handle?
>>
>
> I think this is a bug on the client side, the media type is null and it
> should not be. Could you log an issue?
>
> Paul.
>
>> Thanks,
>> Julio
>>
>> jersey client exception trace:
>> Exception in thread "main" java.lang.NullPointerException
>> at
>> com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General.isSupported(XMLRootElementProvider.java:89)
>> at
>> com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.isReadable(AbstractRootElementProvider.java:73)
>> at
>> com.sun.jersey.core.spi.factory.MessageBodyFactory._getMessageBodyReader(MessageBodyFactory.java:214)
>> at
>> com.sun.jersey.core.spi.factory.MessageBodyFactory.getMessageBodyReader(MessageBodyFactory.java:200)
>> at
>> com.sun.jersey.client.impl.urlconnection.URLConnectionClientHandler$URLConnectionResponse.getEntity(URLConnectionClientHandler.java:148)
>> at
>> com.sun.jersey.client.impl.urlconnection.URLConnectionClientHandler$URLConnectionResponse.getEntity(URLConnectionClientHandler.java:138)
>> at
>> com.sun.jersey.api.client.WebResource.handle(WebResource.java:510)
>> at com.sun.jersey.api.client.WebResource.get(WebResource.java:140)
>>
>> ---------------------------------------------------------------------
>> 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
>
>