users@jersey.java.net

Re: [Jersey] Spell out generics limitations?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 01 Sep 2009 18:44:00 +0200

On Sep 1, 2009, at 5:11 PM, Laird Nelson wrote:

> From looking through the archives, I am led to believe that it is a
> bad idea--or perhaps an impossible idea--to have a Resource that
> generates a parameterized type as one of the formats it @Produces.
>
> So, for example, I have learned that one cannot declare a Resource
> like this (typed from memory off the cuff so forgive syntax errors):
>
> public class SomeResource<T extends SomeTypeAnnotatedWithJAXB> {
> @GET
> @Produces(MediaType.APPLICATION_XML)
> public Collection<T> getStuff() {
> // etc.
> }
> }
>
> ...because Jersey has no way of knowing what T might be, and hence
> has no idea how to spit out appropriate XML. I gleaned most of this
> from http://n2.nabble.com/JSON-not-serializable-for-generic-types-tp3541751p3552413.html
> .
>
> Is this assumption largely correct?

Yes.


> I assume it's true of any parameterized type, yes? And is true
> whether one uses JSON or application/xml?
>

Yes.

I will implement support for a type T, but types like T[],
Collection<T> and Map<T, Collection<V>> are harder to resolve because
it is difficulty creating the concrete parameterized type definitions.
If anybody knows of any mechanism to help aid this i am all ears!


> Background: I was hoping to put together a generic DAO that could
> return collections of things without having to create a new resource
> for every object that I wanted DAO functionality for.
>
> Thanks for any insight. In case you can't tell, I am brand new to
> JAX-RS, and am looking for the right place to jump in; please feel
> free to direct me to wherever I need to go. (I have to say as a
> point of note that the Jersey samples all kind of surprised me: most
> that had anything to do with collections of things seemed to create
> JSON arrays by hand. Is that really the way to go?)
>

We need more samples :-) or those samples could be modified f
appropriate. Issues/patches/contributions welcome!

Paul.