users@jersey.java.net

Re: [Jersey] Exception-Handling null request

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Tue, 10 Aug 2010 18:06:27 +0200

Hi,

For JSON/JAXB integration using XMLStreamReader we need to catch
IllegalStateException in addition to JAXBException and
UnmarshalException, and re-throw as WebApplicationException with a 400
status code.

Can you log an issue?

You can use an exception mapper to map the IllegalStateException to a
response but this is a little hacky as you do not know if such an
exception has been produced from JAXB or not.

To work around this you can EntityHolder:

    https://jersey.dev.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/core/provider/EntityHolder.html

    @POST
    @Path("receive")
    @Consumes("application/json")
    @Produces("application/json")
    public synchronized Response receiveAdress(EntityHolder<Adress>
adress){

           .....
           return response;
    }

Paul.

On Aug 10, 2010, at 5:19 PM, ZigZag wrote:

>
> Hi,
>
> i've a problem with the exception handling of null request. That
> means if my
> service got a correct json object, everything works fine. But if i
> send a
> null object to the service the client got a 500 (internal server
> error)
> response. On the server side i got the error
>
>
> 10.08.2010 16:53:48 com.sun.jersey.spi.container.ContainerResponse
> mapMappableContainerException
> SCHWERWIEGEND: The RuntimeException could not be mapped to a
> response,
> re-throwing to the
> HTTP container
> java.lang.IllegalStateException: reader must be on a START_ELEMENT
> event, not a 8 event
> at
> com
> .sun
> .xml
> .bind
> .v2
> .runtime
> .unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:352)
> at
> com
> .sun
> .xml
> .bind
> .v2
> .runtime
> .unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:339)
> at
> com
> .sun
> .jersey
> .json
> .impl
> .BaseJSONUnmarshaller
> .unmarshalJAXBElementFromJSON(BaseJSONUnmarshaller.java:103)
>
>
> Sure, it make sence that the server can't unmarshal the json string
> to an
> object because it's null, but now my question, is it possible to
> catch the
> exception and to send a more detailed response?
>
>
>
> @POST
> @Path("receive")
> @Consumes("application/json")
> @Produces("application/json")
> public synchronized Response receiveAdress(Adress adress){
>
> .....
> return response;
> }
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/Exception-Handling-null-request-tp5393493p5393493.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>