[英]RESTFUL webservice spring, XML in stead of JSON?
I am trying to return an object as XML in spring, exactly like this guide: http://spring.io/guides/gs/rest-service/ 我试图在春天将对象作为XML返回,就像本指南一样: http : //spring.io/guides/gs/rest-service/
Except that I want the object to return as xml instead of JSON. 除了我希望对象以xml而不是JSON的形式返回。
Anyone know how I can do that? 谁知道我怎么能这样做? Does Spring have any dependancies that can do this as easily for XML? Spring是否有任何依赖可以轻松地为XML做到这一点? Or, do I need to use a marshaller and then return the xml file some other way? 或者,我是否需要使用marshaller然后以其他方式返回xml文件?
Spring supports JSON by default, but to support XML as well, do these steps - Spring默认支持JSON,但为了支持XML,请执行以下步骤 -
@XmlRootElement(name = "response")
@XmlAccessorType(XmlAccessType.FIELD) => this is important, don't miss it.
public class Response {
@XmlElement
private Long status;
@XmlElement
private String error;
public Long getStatus() {
return status;
}
public void setStatus(Long status) {
this.status = status;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
}
@RequestMapping(value = "/api", method = RequestMethod.POST, consumes = {"application/xml", "application/json"}, produces = {"application/xml", "application/json"})
public 上市
public Response produceMessage(@PathVariable String topic, @RequestBody String message) {
return new Response();
}
If you use JAXB annotations in your bean to define @XmlRootElement
and @XmlElement
then it should marshall it xml. 如果在bean中使用JAXB注释来定义@XmlRootElement
和@XmlElement
那么它应该将其编组为xml。 Spring will marshall the bean to xml when it sees: Spring会在看到bean时将bean编组为xml:
Follow this sample to know more: 请按照此示例了解更多信息:
http://www.mkyong.com/spring-mvc/spring-3-mvc-and-xml-example/ http://www.mkyong.com/spring-mvc/spring-3-mvc-and-xml-example/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.