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