Spring 4 REST 程序使用 JAXB 返回 XML 列表
我正在尝试使用 Spring 4 实现 REST 服务.
I am trying to implement a REST service using Spring 4.
REST 方法将返回 XML 格式的客户对象列表.该应用程序是注释驱动的.
The REST method will return a list of customer objects in XML. The application is annotation-driven.
对于 XML,我使用了 JAXB 注释.据我了解,Spring 在找到 JAXB 注释时会立即使用Jaxb2RootElementHttpMessageConverter".
For XML, I have used JAXB annotations. As per my understanding, Spring will use "Jaxb2RootElementHttpMessageConverter" out-of-box when it finds JAXB annotations.
客户 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;
}
}
我已将 REST 服务类注释为 @RestController(根据 Spring 4)
I have annotated the REST service class as @RestController (as per Spring 4)
以 XML 格式返回客户列表的 REST 方法:
@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;
}
点击网址时的预期结果:
<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>
我得到了什么:
HTTP 406:此请求标识的资源只能生成具有根据请求接受"标头不可接受的特征的响应.
HTTP 406 : The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
注意:
当我尝试以 XML 格式返回 Customer 对象时,它按预期工作.但是,我无法返回 XML 中的客户对象列表.
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.
该应用程序是使用 java 7 开发的,它在 Tomcat 7 上运行.
The application is developed using java 7 and it runs on Tomcat 7.
需要帮助.谢谢.
推荐答案
我能够生成客户列表的 XML.
I was able to generate an XML of the list of customers.
首先创建一个通用包装类(其背后的意图是使用这个通用类来传递任何类的对象列表).
First create a generic wrapper class (The intent behind this is to use this generic class to pass a list of objects of any class).
通用包装类:
@XmlRootElement
@XmlSeeAlso({Customer.class})
public class EntityList<T> {
private List<T> listOfEntityObjects;
public EntityList() {
listOfEntityObjects = new ArrayList<T>();
}
public EntityList(List<T> listOfEntityObjects) {
this.listOfEntityObjects = listOfEntityObjects;
}
@XmlAnyElement
public List<T> getItems() {
return listOfEntityObjects;
}
修改后的 REST 方法:
@RequestMapping(value="/customer-list.xml",produces="application/xml")
public EntityList<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);
EntityList<Customer> listOfCustomers =
new EntityList<Customer> (customerList);
return listOfCustomers;
}
XML 响应:
<entityList>
<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>
</entityList>
注意:
生成的XML的根元素是entityList(取自包装类的名字)
The root element of the generated XML is entityList (which is taken from the name of the wrapper class)
但是,我希望 XML 根元素应该是实体名称的复数形式,即 customers 在这种情况下.
However, I would like that the XML Root Element should be the plural of the entity name i.e customers in this case.
有什么想法或建议吗?
相关文章