users@jersey.java.net

Re: [Jersey] Controlling name of outermost tag when returning a list?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 07 Oct 2009 11:16:13 +0200

On Oct 6, 2009, at 9:44 PM, Moises Lejter wrote:

> Hmm - I may be missing something....
> With the code below:
> - /list/asChild returns
> <items/>
> without showing the contents of the array

I think that is because JAXB does not know what to do with something
that extends ArrayList. It is necessary to add some JAXB annotated
methods.


> - /list/asWrapper returns
> <items>
> <values>
> <id>0</id>
> </values>
> </items>
> and (a) I get two wrappers around my list, and (b) I lose the
> <item> tag (?!)
>

Try using the following:

  @XmlRootElement(name="items")
   static public class ListWrapper {
     public List<Value> item = new ArrayList<Value>(); // change field
name
     ListWrapper() {}
     ListWrapper( Value value ) {
       values.add(value);
     }
   }
   @XmlRootElement(name="item") // this will only apply to the root
element of an XML document
   static public class Value {
     public int id;
   }

or:

  @XmlRootElement(name="items")
   static public class ListWrapper {
     @XmlElement(name="item")
     public List<Value> values = new ArrayList<Value>();
     ListWrapper() {}
     ListWrapper( Value value ) {
       values.add(value);
     }
   }
   @XmlRootElement(name="item")
   static public class Value {
     public int id;
   }

Paul.

> I suspect it may be something really simple - but I don't see it :-
> (. Help?
>
> Moises
>
> ---cut here---
>
> @Path("/list")
> public class Lister {
> @GET @Path("/asChild")
> public ValueList valuesAsChild() {
> return
> new ValueList( new Value() );
> }
> @GET @Path("/asWrapper")
> public ListWrapper valuesAsWrapper() {
> return
> new ListWrapper( new Value() );
> }
> @XmlRootElement(name="items")
> static public class ValueList extends ArrayList<Value>
> implements List<Value> {
> ValueList() {}
> ValueList( Value value ) {
> add(value);
> }
> }
> @XmlRootElement(name="items")
> static public class ListWrapper {
> public List<Value> values = new ArrayList<Value>();
> ListWrapper() {}
> ListWrapper( Value value ) {
> values.add(value);
> }
> }
> @XmlRootElement(name="item")
> static public class Value {
> public int id;
> }
> }
>