springmvc support for JSON data

springmvc of JSON data support

JSON (JavaScript Object Notation) is a lightweight data interchange format. JSON a fully language independent text format, these characteristics make JSON ideal data exchange language.

JSON may JavaScript a set of data transfer objects represented as a string, then the string can be passed between functions easily, or string from the asynchronous application will Web client side to the server-side programs. This string looks a bit odd, but JavaScript is easy to explain it, but JSON can represent more than " name / value pair " more complex structures. For example, the list may represent a simple and complex arrays of objects, not just the keys and values.

JSON data format interface calls, HTML pages more common, JSON format is relatively simple, relatively easy to resolve, it is very common to use. In SpringMVC , but also support for JSON parsing and conversion data

 

1 JSON represents the name / value pairs:

In its simplest form, this may be following JSON represents " name / value pairs " : { "firstName": "David"} , this very basic example, and in fact higher than the equivalent plain text " name / value pairs " take up more space: firstName = Brett , however, when a plurality of " name / value pairs " when strung together, JSON will reflect its value. First of all, you can create a more " name / value " records, such as:

{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }

From the grammatical point of view, which is the " name / value pair " compared to no great advantage, but in this case the JSON easier to use and more readable.

For example, it is clear that these three values are part of the same record , braces so that these values have some connection.

 

2 JSON array representing:

When the need to represent a set of values, the JSON only improve readability, but also can reduce the complexity. For example, suppose you want to represent a list of names. In XML , the need for many of the start and end tags; If a typical name / value pairs, then the need to establish a proprietary data format, or to modify the key name person1-firstName such forms.

If you use JSON , simply record multiple grouped together with curly braces:

{ "people": [

{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },

{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},

{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }

]}

Demand: The client sends JSON data format, the server side JSON converts the format type of data object, then returns JSON -formatted data, then the client data content for display.

Namely: request and return are JSON -formatted data.

 

3 development environment to build :

1. maven dependency is introduced:

  <!--对json的支持-->
   <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.9</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.9</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.9.9</version>
    </dependency>

2. Add springmvc support for the json

<bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonHttpMessageConverter" />
            </list>
        </property>
    </bean>

    <bean id="jsonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>application/json;charset=UTF-8</value>
            </list>
        </property>
    </bean>


NOTE: <mvc: annotation-driven> </ mvc: annotation-driven> may be

Configuring the controller layer codes:

@RequestBody receiving reception json data , the json data automatic packaging Model .

findStuById(@RequestBody Stu stu)

@ResponseBody the background model convert json object, and returns to the page.

  @RequestMapping("/findStuById2.do")
    public @ResponseBody Student findStuById2(){
        Student stu=stuService.FindStu(1);
        System.out.println(stu);
        return stu;
    }

3. Run the test: http: // localhost: 8080 / findStuById2.do

 

 

 

 

 

 

 

Published 84 original articles · won praise 0 · Views 709

Guess you like

Origin blog.csdn.net/qq_38405199/article/details/102865771