How to serialize objects directly to the root node without a variable name when Spring MVC returns to the JSON view

Source: Internet
Author: User

How to serialize an object directly to a JSON message without a variable name as the root node when Spring MVC returns to the JSON viewProblem

The problem is a bit of a mouthful, in fact, when using spring MVC, how to map objects to JSON messages without using the object as the root node of the JSON. Even with the @responsebody effect.
For example, by default, Java objects are saved by using Modelandview's AddObject (key,object) or Modelmap AddAttribute (key,object). When the view parser to srping is parsed into JSON, the name of the root node of the JSON is used as key, and if no key is passed, a camel root node name is generated using the class name, like this:

{    "objectName": {        "id": 123,        "name": "zhangsan"    }}

If the controller is annotated with @responsebody, the root node is not generated, and the resulting JSON message is this:

{    "id": 123,    "name": "zhangsan"}

When the controller in the project is mixed with @responsebody and Modelandview, the returned message format is inconsistent, which is inconvenient for the front end solution (some places are read from the root node and some are read directly from the returned JSON object).

Solution Solutions

Before Baidu did not find a description of similar problems, and then Google from the StackOverflow found in the solution, it seems that Google for me to find the problem of higher probability. :)
The solution is simple, which is to set the Extractvaluefromsinglekeymodel of the JSON view in spring to true.
Spriing3.1 before the JSON view is org.springframework.web.servlet.view.json.MappingJacksonJsonView
The version after 3.1 is recommended for use as a JSON view org.springframework.web.servlet.view.json.MappingJackson2JsonView .
The following is the configuration in the Spring MVC container, which uses the XML configuration:

<!--spring3.1以下配置--><bean   class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">        <property name="extractValueFromSingleKeyModel" value="true" /></bean>
<!--spring3.1以上配置--><bean   class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">        <property name="extractValueFromSingleKeyModel" value="true" /></bean>

This way, no @responsebody or Modelandvuiew return JSON, there is no root node.
The following is a description of this property by the Spring API:

public void Setextractvaluefromsinglekeymodel (Boolean Extractvaluefromsinglekeymodel)
Set whether to serialize models containing a single attribute as a map or whether to extract the single value from the MoD El and serialize it directly.
The effect of setting this flag was similar to using Mappingjackson2httpmessageconverter with an @ResponseBody Request-hand Ling method.

It means controlling the direct serialization of an object or wrapping it with a property, which is similar to using the @responsebody

Reference

1.stackoverflow Posts: http://stackoverflow.com/questions/9517189/ Spring-mvc-and-jackson-mapping-do-not-return-the-root-element-in-json
API document for 2.Spring: http://docs.spring.io/spring/docs/current/javadoc-api/

How to serialize objects directly to the root node without a variable name when Spring MVC returns to the JSON view

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.