users@jersey.java.net

understanding content types and resource methods

From: John Calcote <john.calcote_at_gmail.com>
Date: Sat, 19 Dec 2009 10:33:57 -0700

Hi,

I'm having trouble understanding how content-types properly relate to
resource methods.

I'm writing a web service that uses JAXB bindings to send java objects
in XML format over a ReSTful interface. I've defined two java objects in
my message library: MessageA and MessageB. I send these messages with
the same content type:

"application/vnd.com.example-v1+xml"

On the web service (also implemented using Jersey and JAXB), I have two
resource methods:

     @POST
     @Consumes("application/vnd.com.example-v1+xml")
     public Response postMessageA(JAXBElement<MessageA> msg) {
         MessageA msgA = msg.getValue();
         ...
     }

     @POST
     @Consumes("application/vnd.com.example-v1+xml")
     public Response postConnectorUpdatedEvent(JAXBElement<MessageB> msg) {
         MessageB msgB = msg.getValue();
         ...
     }

When I run this service, I get a startup error as the context is
initialized indicating that I have ambiguous method resources of type
HTTP POST, and the mime types of both are listed (the same, of course).

My question is this: Clearly, I'm not supposed to use the same mime type
in these two POST resource methods. What is the proper design for such a
scenario? Should I have unique mime-types per message, or should I have
a single resource method that somehow handles both messages.

The first option would be simpler, but is it proper? The second option
would require some JAXB gyrations of which I'm not yet familiar, such as
determining the message type before casting to the proper java object
class, or handing a base Message class from which both are derived, and
using an internal field as a subclass selector (yuck). I'd really like
Jersey to figure out the exact message type for me.

Thanks in advance,

John Calcote
Sr. Software Engineer
Novell, Inc.