On May 22, 2009, at 8:46 AM, Chad McHenry wrote:
> Is there a way to serialize a List<Integer> via JSON-JAXB?
>
No, you will need to wrap the integer around a JAXB element object.
For XML support it would not be clear what the XML root element would
be and if each item of the integer list would be wrapped around an
element, or would consist of one element with comma separated values.
> If the list is a member of an @XmlRootElement (i.e. Foo below), which
> is registered with the JAXBContextResolver, I can return a List<Foo>
> and the nested List<Integer> will marshal fine.
>
Yes.
> Adding ArrayList (Arrays$ArrayList is not accessible) does not work,
> nor does subclassing ArrayList to add the @XmlRootElement and register
> with JAXBContextResolver.
The for latter, you might need to add a couple more annotations to
help JAXB know what to do with the List.
For such cases it might be better to stick with just JSON and use the
direct object to JSON supported offered by Jackson, which supplies the
message body writers and may support your case. Tatu would know more.
Paul.
> $ > curl -X GET -H 'Content-type: application/json'
> http://localhost/pinakes/api/media/raw
> A message body writer for Java type, class java.util.Arrays$ArrayList,
> and MIME media type, application/json, was not found
>
> $ > curl -X GET -H 'Content-type: application/json'
> http://localhost/pinakes/api/media/nested
> {"list":["1","2","3"]}
>
> $ > curl -X GET -H 'Content-type: application/json'
> http://localhost/pinakes/api/media/nestedList
> [{"list":["1","2","3"]},{"list":["1","2","3"]}]
>
> $ > curl -X GET -H 'Content-type: application/json'
> http://localhost/pinakes/api/media/subclass
> null
>
> === Foo.java ===
> @XmlRootElement
> public class Foo {
> public List<Integer> list = Arrays.asList(1,2,3);
> }
> === MyArray.java ===
> @XmlRootElement
> public class MyArray<E> extends ArrayList<E> {
> }
> === Resource.java ===
> ...
> @GET
> @Path("/raw")
> public List<Integer> testRaw() {
> return Arrays.asList(1,2,3);
> }
> @GET
> @Path("/nested")
> public Foo testNested() {
> return new Foo();
> }
> @GET
> @Path("/nestedList")
> public List<Foo> testNestedList() {
> return Arrays.asList(new Foo(),new Foo());
> }
> @GET
> @Path("/subclass")
> public List<Integer> testSubclass() {
> List<Integer> list = new MyArray<Integer>();
> list.add(1);
> list.add(2);
> return list;
> }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>