我正在尝试使用Spring 4实现REST服务.
REST方法将返回XML中的客户对象列表.该应用程序是注释驱动的.
对于XML,我使用了JAXB注释.根据我的理解,Spring会在找到JAXB注释时使用"Jaxb2RootElementHttpMessageConverter"开箱即用.
客户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;
}
}
Run Code Online (Sandbox Code Playgroud)
我已将REST服务类注释为@RestController(根据Spring 4)
REST方法以XML格式返回客户列表:
@RequestMapping(value="/customer-list.xml",produces="application/xml") …Run Code Online (Sandbox Code Playgroud)