Java RESTFUL webservice spring,XML 代替 JSON?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18920770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RESTFUL webservice spring, XML in stead of JSON?
提问by mikhail90
I am trying to return an object as XML in spring, exactly like this guide: http://spring.io/guides/gs/rest-service/
我试图在 spring 中以 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? Or, do I need to use a marshaller and then return the xml file some other way?
有谁知道我怎么能做到这一点?Spring 是否有任何依赖项可以轻松地对 XML 执行此操作?或者,我是否需要使用编组器然后以其他方式返回 xml 文件?
采纳答案by Juned Ahsan
If you use JAXB annotations in your bean to define @XmlRootElement
and @XmlElement
then it should marshall it xml. Spring will marshall the bean to xml when it sees:
如果您在 bean 中使用 JAXB 注释进行定义@XmlRootElement
,@XmlElement
那么它应该将其编组为 xml。Spring 会在看到以下内容时将 bean 编组为 xml:
- Object annotated with JAXB
- JAXB library existed in classpath
- “mvc:annotation-driven” is enabled
- Return method annotated with @ResponseBody
- 用 JAXB 注释的对象
- 类路径中存在 JAXB 库
- “mvc:annotation-driven”已启用
- 用@ResponseBody 注释的返回方法
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/
回答by Vikram Gulia
Spring supports JSON by default, but to support XML as well, do these steps -
Spring 默认支持 JSON,但要同时支持 XML,请执行以下步骤 -
- In the class you plan to return as response, add xml annotations. for e.g.
- 在您计划作为响应返回的类中,添加 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;
}
}
- Add produces and consumes to your @RequestMappingon the restful method like below, this helps in making sure what kind of responses and request you support, if you only want response as xml, only put produces = "application/xml".
- 在下面的restful方法上将生产和消费添加到您的@RequestMapping,这有助于确定您支持什么样的响应和请求,如果您只想要作为xml的响应,只需放置produces =“application/xml”。
@RequestMapping(value = "/api", method = RequestMethod.POST, consumes = {"application/xml", "application/json"}, produces = {"application/xml", "application/json"})
public
民众
- Then, make sure you return the response object from your method call like below, you can add @ResponseBody just before return type but in my experience, my app worked fine without it.
- 然后,确保你从你的方法调用中返回响应对象,如下所示,你可以在返回类型之前添加 @ResponseBody 但根据我的经验,我的应用程序在没有它的情况下运行良好。
public Response produceMessage(@PathVariable String topic, @RequestBody String message) {
return new Response();
}
- Now, if you are supporting multiple produces types, then based on what client sent as the Acceptin the HTTP request header, the spring restful service will return that type of response. If you only want to support xml, then only produce 'application/xml' and the response will always be xml.
- 现在,如果您支持多种生产类型,那么根据客户端在 HTTP 请求标头中作为Accept发送的内容,spring restful 服务将返回该类型的响应。如果您只想支持 xml,则只生成 'application/xml' 并且响应将始终为 xml。