users@jersey.java.net

Re: [Jersey] understanding content types and resource methods

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 29 Dec 2009 10:20:10 +0100

On Dec 21, 2009, at 3:26 PM, Marc Hadley wrote:

> 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.
>

Note that to use Object you need to also define a
ContextResolver<JAXBContext> implementation that returns a JAXBContext
for the Class of Object.

Paul.