users@jersey.java.net

Can a resource support an optional JAXB request entity?

From: Tim Pesce <tpesce_at_foxfish.com>
Date: Thu, 13 Nov 2008 13:35:35 -0700

I'm trying to implement something similar to the WebDAV PROPFIND
method. Specifically I'd like to support the case where a request body
is optional, and the absence of a body has implied semantics.

If I use byte[] or InputStream or one of the other JAX-RS supported
types, requests with an empty body result in an entity with
characteristics I would expect (array w/ length 0, InputStream w/ no
data). However, if my entity is a JAXB annotated type then requests
with an empty body trigger error (400) responses prior to my resource
method being invoked. After doing a bit of playing w/ an exception
mapper, it seems that an UnmarshalException is being wrapped and
bubbled up:

javax.ws.rs.WebApplicationException: javax.xml.bind.UnmarshalException
  - with linked exception:
[org.xml.sax.SAXParseException: Premature end of file.]
        at
com
.sun
.jersey
.impl
.provider
.entity
.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:
93)
        at
com
.sun
.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:
353)

I'm not sure if this is my own error, a general limitation, or perhaps
a bug. Here's the entity and resource method I'm using for a simple
reproduction case:

     @XmlRootElement(name = "entity")
     public class Entity
     {
     }

     @CUSTOM
     @Consumes("application/xml")
     public Response test(Entity entity)
     {
         [...]
     }

I would expect Jersey to be passing a null to my resource method in
these cases, but I'm not sure if my expectations are correct. Are
there other approaches for handling this scenario? I've tried
implementing a resource method specifically for the empty case, but I
haven't found anything that works.

Tim