users@jersey.java.net

RE: [Jersey] RE: Set the root when returning a collection

From: Jeremy Whitlock <jwhitlock_at_collab.net>
Date: Fri, 14 Nov 2008 14:36:58 -0800

Hello all,
        Alright, I have created a simple Jersey app that utilizes JAXB
to take collections of objects and return a JSON/XML representation for
the collections. What I found was actually pretty interesting and here
is a brief summary:

1) Generic list support: While Jersey does not include an element for
each array element, it shouldn't be necessary. (For example:
List<Person>.)
2) Array support: Jersey seemed to have a problem taking an array of
objects and serializing them to JSON/XML. I kept getting errors about
no content writer being available. (For example: Person[].)
3) Mixed list support: Same as scenario #2. No content writer
available.

With regard to scenarios #2, if I return a single object instead of an
array of objects, the serialization works fine so JAXB should be able to
serialize an array of objects all the same type.

Attached is the sample Maven project that demonstrates this
functionality. To see an example of the output, just run "mvn compile
exec:java" and you'll get some output to view for relating to this
thread's discussion. (When running the example, you'll see that the
accesses for places.json and places.xml are commented out because they
just don't work.) There is a lot of code duplication since I was having
a problem getting a Resource to serialize properly. (I emailed this
list about this already.) I also know this could had been done using
unit tests but sometimes, seeing is believing.

Take care,

Jeremy