users@jersey.java.net

Re: [Jersey] building Response from a collection?

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Wed, 07 Oct 2009 09:17:16 -0400

This is a problem with type erasure[1]. When using Response with a
generic type as the entity (List<EntityName> in this case) you need to
capture the generic entity type using GenericEntity like this:

List<EntityName> list = entityA.getBEntities();
GenericEntity<List<EntityName>> entity = new
GenericEntity<List<EntityName>>(list) {};
return Respones.ok(entity).build();

Without this the runtime cannot know that you are returning a list of
EntityName so it can't work out how to serialize it.

Marc.

[1] http://java.sun.com/docs/books/tutorial/java/generics/erasure.html

On Oct 7, 2009, at 8:38 AM, Zoltan Arnold NAGY wrote:

> Hey all,
>
> This might have been asked earlier, but google didn't help me out. I
> might have missed
> a keyword.. :)
>
> I'm trying to return a list of entities coming from a JPA entity. As
> far as I do it this
> way:
>
> @GET
> @Produces("application/json")
> public List<EntityName> get() {
> // look up entityA
> return entityA.getBEntities();
> }
>
> it works fine. However, I wanted to introduce
> error handling (return a 404 using Response.status(404).build()
> on a specified exception), so I changed the code:
>
> @GET
> @Produces("applicatoin/json")
> public Response get() {
> // do the same, but
> return Respones.ok(entityA.getBEntities()).build();
> }
>
> this way I always get an exception, namely:
>
> [java] SEVERE: A message body writer for Java type, class
> org.eclipse.persistence.indirection.IndirectList, and MIME media
> type, application/json, was not found
>
>
> I guess in the first case, JAXB somehow wraps my data, and in the
> latter it can't for one reason or another.
> Is there a way I could mimic the same behaviour?
>
> Thanks,
> Zoltan
>
>
> __________ Information from ESET Smart Security, version of virus
> signature database 4486 (20091007) __________
>
> The message was checked by ESET Smart Security.
>
> http://www.eset.com
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>