users@jersey.java.net

Re: entity providers for collections?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 02 Apr 2008 12:08:35 +0200

FSauer_at_dsthealthsolutions.com wrote:
>
>
> How does one write JAXB entity providers for collections? Wrapping them
> in a class works fine, but
> I want to be able to return List<SomeType> from resource methods but I
> can't figure out how to register the providers.
> type-erasure makes this hard..... if not impossible. How do you
> distinguish multiple providers for ArrayList if the element type
> is not available at runtime??? Any help is appreciated,
>

Currently we cannot support it.

The isWriteable method could have the instance as a parameter, then one
can do some checking on the classes of the entries in the collection,
but that is not very efficient. As you say because of type erasure we
are in a very tricky situation.

Marc and I tried to make Response and Response.ResponseBuilder generic
but gave up because we could not find a way to correctly define the
generic types and work in a type safe manner. If anyone can find such a
solution i will buy them many beers!

A hack is to allow Response to be generic but when setting the entity
the type is ignored:

   @GET
   public Response<Collection<Bean>> get() {
      Collection<Bean> c = ...
      return Response.ok(c).build();
   }

Thus at least it allows the declaration of the generic type of the
entity, but it does mean the instance set in the response may be
different from that type. It is also important that the following:

   @GET
   public Collection<Bean> get() {
       Collection<Bean> c = ...
       return c;
   }

has consistent behavior if changed to use Response.

Paul.

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109