users@jersey.java.net

Re: [Jersey] Re: Type argument in MessageBodyWriter methods

From: Marc Hadley <marc.hadley_at_oracle.com>
Date: Wed, 14 Apr 2010 16:30:16 -0400

On Apr 14, 2010, at 4:17 PM, ljnelson wrote:

> On Wed, Apr 14, 2010 at 2:46 PM, Marc Hadley-2 [via Jersey]
> <[hidden email]> wrote:
> > On Apr 14, 2010, at 1:32 PM, ljnelson wrote:
> > The Type argument would be an instance of ParameterizedType whose getRawType
> > would return List.class and whose getActualTypeArguments() would return an
> > array of [String.class]. (I think).
>
> Another question on this sort of thing: the T that comes in to
> writeTo() in MessageBodyWriter is accompanied by a Class<?>. Was this
> supposed to be Class<T>? Or was the intent that any old class could
> show up here?
>
The isWritable method is responsible for filtering the types that the writeTo will be called for so you might get a subclass of T if you decide to support that.

> This leads to my next question. The first thing I need to do in my
> "chaining" MessageBodyWriter is to ask Providers for another one that
> matches. I'm armed at this point with a T, and a Class<?>. But
> Providers#getMessageBodyWriter() needs a Class argument that has as
> its parameter T. It would seem that I can't guarantee this. What's
> your advice?
>
If you use Class<?> then you'll get a MessageBodyWriter<?> which should still work fine for you. I'd probably go that way for a chaining writer that supports multiple types.

Marc.