users@jersey.java.net

Re: [Jersey] understanding content types and resource methods

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Mon, 21 Dec 2009 09:26:36 -0500

Dispatching of HTTP requests to Java methods is based on 4 things:

(i) The URI path
(ii) The HTTP method
(iii) The media type of the request (Content-Type header)
(iv) The media type requested for the response (Accept header)

The dispatch algorithm is described in all its gory details here:

https://jsr311.dev.java.net/nonav/releases/1.1/spec/spec3.html#x3-340003.7

Dispatch based on the deserialized Java type isn't supported.

As suggested earlier in the thread you can use different paths (via @Path) for each type of message or different media types (via @Consumes). Having one service consume multiple different media types is fine BTW.

An alternative is to use a single wrapper element for all types of event that can contain each of your different event messages. e.g.:

<wrapper>
  <event1>...</event1>
</wrapper>

You can then switch in the method code to determine the type of event received.

Another alternative is to use Object as your method parameter type and then use instanceof to determine the message received.

HTH,
Marc.

On Dec 19, 2009, at 12:33 PM, John Calcote wrote:

> 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.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>