users@jersey.java.net

Re: [Jersey] JSON not serializable for generic types :(

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 31 Aug 2009 11:33:52 +0200

Hi,

As previously discussed T[] or Collection<T> is not supported for
request/response entities or parameters, and is likely to be very hard
in general to implement properly (e.g. Map<K, Collection<V>>), so i
doubt it will ever be supported.

Even T is not supported for a response if T resolves to say
Collection<MyBean>, because currently T is only resolved for request
entities. That i can fix along with injection, as previously
discussed, by ensuring the abstract model resolves, given the
limitations, T to a concrete type.

Paul.

On Aug 29, 2009, at 2:15 PM, Felipe Gaścho wrote:

> this code works fine with XML and JSON formats:
>
> @GET
> @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
> @Path("/{offset}/{limit}")
> public Collection<PujInstitutionRoleEntity> selectAll(
> @PathParam("offset") int offset, @PathParam("limit") int limit) {
> return facade.readAll(PujInstitutionRoleEntity.class, offset,
> limit);
> }
>
> this other code doesn't work for JSON formats:
>
>
> public abstract class PujGenericResource<T extends
> AbstractArenaEntity, PK> {
> protected abstract PujEntityFacade<T> getFacade();
>
> private final Class<T> genericType;
>
> @SuppressWarnings("unchecked")
> @GET
> @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
> @Path("{offset}/{limit}")
> public T[] selectAll(@PathParam("offset") int offset,
> @PathParam("limit") int limit) {
> Collection<T> results = getFacade().readAll(genericType, offset,
> limit);
> T[] response =
> results.toArray((T[]) Array.newInstance(genericType, results
> .size()));
> return response;
> }
>
>
> -----------
>
> This means I am forced to repeat myself copying the method for all
> resources ... ?
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>