title: Spring 4 REST program to return a List in XML using JAXB
body: <p>I am trying to implement a REST service using Spring 4.</p>
  
  <p>The REST method will return a list of customer objects in XML.
  The application is annotation-driven.</p>
  
  <p>For XML, I have used JAXB annotations.
  As per my understanding, Spring will use "Jaxb2RootElementHttpMessageConverter" out-of-box when it finds JAXB annotations.</p>
  
  <p><strong>The Customer POJO:</strong></p>
  
  <pre><code>@XmlRootElement(name = "customer")
  public class Customer {
  private int id;
  private String name;
  private List favBookList;
  @XmlAttribute
  public int getId() {
      return id;
  }
  public void setId(int id) {
      this.id = id;
  }
  @XmlElement
  public String getName() {
      return name;
  }
  public void setName(String name) {
      this.name = name;
  }
  
  @XmlElementWrapper(name = "booklist")
  @XmlElement(name="book")
  public List getFavBookList() {
      return favBookList;
  }
  public void setFavBookList(List favBookList) {
      this.favBookList = favBookList;
  }
  } 
  </code></pre>
  
  <p>I have annotated the REST service class as @RestController (as per Spring 4)</p>
  
  <p><strong>The REST method to return a list of customers in XML :</strong></p>
  
  <pre><code>@RequestMapping(value="/customer-list.xml",produces="application/xml")
  public  List&lt;Customer&gt; getCustomerListInXML(){
      List&lt;Customer&gt; customerList = new ArrayList&lt;Customer&gt;();
      Customer customerObj1 = new Customer();
      customerObj1.setId(1);
      customerObj1.setName("Vijay");
      ArrayList&lt;String&gt; favBookList1 = new ArrayList&lt;String&gt;();
      favBookList1.add("Book1");
      favBookList1.add("Book2");
      customerObj1.setFavBookList(favBookList1);
      customerList.add(customerObj1);
      Customer customerObj2 = new Customer();
      customerObj2.setId(2);
      customerObj2.setName("Rajesh");
      ArrayList&lt;String&gt; favBookList2 = new ArrayList&lt;String&gt;();
      favBookList2.add("Book3");
      favBookList2.add("Book4");
      customerObj2.setFavBookList(favBookList2);
      customerList.add(customerObj2);
      return customerList;
  }
  </code></pre>
  
  <p><strong>The result I expected, when I hit the URL :</strong></p>
  
  <pre><code> &lt;customers&gt;
    &lt;customer id="1"&gt;
     &lt;booklist&gt;
      &lt;book xsi:type="xs:string"&gt;Book1&lt;/book&gt;
      &lt;book xsi:type="xs:string"&gt;Book2&lt;/book&gt;
     &lt;/booklist&gt;
     &lt;name&gt;Vijay&lt;/name&gt;
    &lt;/customer&gt;
    &lt;customer id="2"&gt;
     &lt;booklist&gt;
      &lt;book xsi:type="xs:string"&gt;Book3&lt;/book&gt;
      &lt;book xsi:type="xs:string"&gt;Book4&lt;/book&gt;
     &lt;/booklist&gt;
     &lt;name&gt;Rajesh&lt;/name&gt;
    &lt;/customer&gt;
   &lt;/customers&gt;
  </code></pre>
  
  <p><strong>What I get :</strong></p>
  
  <p>HTTP 406 :
  The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.</p>
  
  <p><strong>Note :</strong></p>
  
  <p>When I try to return a Customer object in XML, it works as expected.
  However, I am unable to return a list of Customer objects in XML.</p>
  
  <p>The application is developed using java 7 and it runs on Tomcat 7.</p>
  
  <p>Need help with this. Thanks.</p>