title: Marshal/Un marshal List objects in Jersey JAX-RS using JAXB
body: <p>Good Morning. Today morning when I am going through Jersey Entity providers <code>MessageBodyReader</code>s and <code>MessageBodyWriter</code>s I came across the following problem.</p>
  
  <p>I want to write a resource method and client that returns a list of custom objects and media type is <code>application/xml</code>.  So I would like to use JAXB (I am new to JAXB). I was able to achieve this by writing my own extended <code>MessageBodyReader</code> and <code>MessageBodyWriter</code>.  But I am afraid of the way I am following.  Just look at the way I implemented:</p>
  
  <p>Resource method:</p>
  
  <pre><code>@Path("productlist/xml")
  @GET
  public RetObjects getProductsXml(){
      List&lt;Product&gt; pList = new ArrayList&lt;Product&gt;();
      pList.add(new Product("1","Dell latitude E6000",2900,500));
      pList.add(new Product("2","Xperia Z2",549,400));
      RetObjects obj = new RetObjects();
      obj.setObject(pList);
      return obj;
  }
  </code></pre>
  
  <p>My custom objects:</p>
  
  <pre><code>@Entity
  @Table (name="PRODUCT")
  @XmlRootElement(name="product")
  public class Product {
  
      @Id
      @Column(name = "CODE")
      private String code;
      ...
      // rest of the fields, constructors, getters and setters
   }
  </code></pre>
  
  <p>Object that wraps my list of custom object:</p>
  
  <pre><code> @XmlRootElement(name = "products")
   @XmlAccessorType (XmlAccessType.FIELD)
   public class RetObjects {
  
       @XmlElement(name = "product")
       private List&lt;Product&gt; object = null;
  
       public List&lt;Product&gt; getObject() {
           return object;
       }
  
       public void setObject(List&lt;Product&gt; object) {
           this.object = object;
       }
   }
  </code></pre>
  
  <p><code>MessageBodyReader/Writer</code> are straight forward just using Jaxb unmarshaller and marshaller over the <code>RetObjects</code> object.</p>
  
  <p>With this implementation it is working fine as expected and i am able to fetch the <code>RetObjects</code> wrapping the list of Products perfectly fine at client.</p>
  
  <p>Here my question is, instead of wrapping my List of Products into a intermediate object, <code>RetObjects</code> in my case, couldn't I marshal and unmarshal List of Products object directly.  If I want to write another service that returns List of Orders, I need to wrap this with one more intermediate object.  What is the right approach to achieve this?  How could I do this without intermediate objects?</p>