spring mvc rest response json and xml

后端 未结 7 1901
無奈伤痛
無奈伤痛 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:09

    Whoa...when you're working with Spring, assume someone else has come up against the same issue. You can dump all the server-side JSON generation, because all you need to do is:

    1. Include the Jackson JSON JARs in your app
    2. Set the RequestMapping return type to @ResponseBody(yourObjectType)

    Spring will auto-magically convert your object to JSON. Really. Works like magic.

    Doc for @ResponseBody: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-responsebody

提交回复
热议问题