spring mvc rest response json and xml

后端 未结 7 1862
無奈伤痛
無奈伤痛 2020-12-28 19:24

I have the requirement to return the result from the database either as a string in xml-structure or as json-structure. I\'ve got a solution, but I don\'t know, if this one

7条回答
  •  一生所求
    2020-12-28 20:05

    If someone using Spring Boot for XML response then add the following dependency,

    
        com.fasterxml.jackson.dataformat
        jackson-dataformat-xml
    
    

    And in the model class you are returning add the @XmlRootElement.

    @Entity
    @Table(name = "customer")
    @XmlRootElement
    public class Customer {
          //... fields and getters, setters
    }
    

    and in your controller add produces="application/xml". This will produce the response in xml format.

提交回复
热议问题