简体   繁体   English

RESTFUL webservice spring,XML代替JSON?

[英]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,请执行以下步骤 -

  1. In the class you plan to return as response, add xml annotations. 在您计划作为响应返回的类中,添加xml注释。 for eg 例如
    @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;
        }
    }
  1. Add produces and consumes to your @RequestMapping on 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,只需要put =“application / xml”。
@RequestMapping(value = "/api", method = RequestMethod.POST, consumes = {"application/xml", "application/json"}, produces = {"application/xml", "application/json"})

public 上市

  1. 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();
}
  1. Now, if you are supporting multiple produces types, then based on what client sent as the Accept in the HTTP request header, the spring restful service will return that type of response. 现在,如果您支持多种产品类型,那么基于客户端在HTTP请求标头中作为Accept发送的内容,spring restful服务将返回该类型的响应。 If you only want to support xml, then only produce 'application/xml' and the response will always be xml. 如果您只想支持xml,那么只生成'application / xml',响应将始终为xml。

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:

  • Object annotated with JAXB 用JAXB注释的对象
  • JAXB library existed in classpath JAXB库存在于classpath中
  • “mvc:annotation-driven” is enabled “mvc:annotation-driven”已启用
  • Return method annotated with @ResponseBody 使用@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/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM