JSON data transmission parameters of SpringMVC

Table of contents

One: JSON ordinary array

Two: JSON object data

Three: Array of JSON objects


       As we said earlier, the more popular development method is asynchronous calls. The front-end and back-end exchange in an asynchronous manner, and the transmitted data uses ==JSON==, so if the front-end sends JSON data, how should the back-end receive it?

For the JSON data type, there are three common ones:

  • json ordinary array (["value1","value2","value3",...])

  • json object ({key1:value1,key2:value2,...})

  • array of json objects ([{key1:value1,...},{key2:value2,...}])

For the above data, how does the front end send and how does the back end receive it?

One: JSON ordinary array

Step 1: Add dependencies to pom.xml

SpringMVC uses jackson by default to handle json conversion, so you need to add jackson dependency in pom.xml

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.0</version>
</dependency>

Step 2: PostMan sends JSON data

Step 3: Enable SpringMVC annotation support

Enable the annotation support of SpringMVC in the configuration class of SpringMVC, which includes the function of converting JSON into objects.

@Configuration
@ComponentScan("com.itheima.controller")
//开启json数据类型自动转换
@EnableWebMvc
public class SpringMvcConfig {
}

Step 4: Add @RequestBody before the parameter
 

//使用@RequestBody注解将外部传递的json数组数据映射到形参的集合对象中作为数据
@RequestMapping("/listParamForJson")
@ResponseBody
public String listParamForJson(@RequestBody List<String> likes){
    System.out.println("list common(json)参数传递 list ==> "+likes);
    return "{'module':'list common for json param'}";
}

Step 5: Start and run the program

 The data of JSON ordinary array has been transferred, how to transfer the data of JSON object data and JSON object array?

Two: JSON object data

We will find that we only need to pay attention to how the request and data are sent? How is the back-end data received?

Sending of requests and data:

{
	"name":"itcast",
	"age":15
}

The backend receives data:  

@RequestMapping("/pojoParamForJson")
@ResponseBody
public String pojoParamForJson(@RequestBody User user){
    System.out.println("pojo(json)参数传递 user ==> "+user);
    return "{'module':'pojo for json param'}";
}

Start program access test

illustrate:

The reason why address is null is that the front end does not pass data to the back end.

If we want address to also have data, we need to modify the data content passed by the front end:

{
	"name":"itcast",
	"age":15,
    "address":{
        "province":"beijing",
        "city":"beijing"
    }
}

Send the request again, and you can see the data in address

Three: Array of JSON objects

How to save multiple POJOs in the collection?

Sending of requests and data:

[
    {"name":"itcast","age":15},
    {"name":"itheima","age":12}
]

The backend receives data:  

@RequestMapping("/listPojoParamForJson")
@ResponseBody
public String listPojoParamForJson(@RequestBody List<User> list){
    System.out.println("list pojo(json)参数传递 list ==> "+list);
    return "{'module':'list pojo for json param'}";
}

Start program access test

 

summary

The implementation steps of SpringMVC receiving JSON data are:

(1) Import jackson package

(2) Use PostMan to send JSON data

(3) Turn on the SpringMVC annotation driver and add the @EnableWebMvc annotation to the configuration class

(4) Add the @RequestBody annotation before the parameters of the Controller method

Knowledge point 1: @EnableWebMvc

name @EnableWebMvc
type == Configuration class annotations ==
Location Above the SpringMVC configuration class definition
effect Enable multiple auxiliary functions of SpringMVC

Knowledge point 2: @RequestBody

name @RequestBody
type ==Formal parameter annotation==
Location SpringMVC controller method parameter definition front
effect Pass the data contained in the request body to the request parameters. This annotation can only be used once for a handler method

The difference between @RequestBody and @RequestParam

  • the difference

    • @RequestParam is used to receive url address pass parameters, form pass parameters [application/x-www-form-urlencoded]

    • @RequestBody is used to receive json data [application/json]

  • application

    • In the later stage of development, the data in json format is mainly sent, and @RequestBody is widely used

    • If you send data in non-json format, use @RequestParam to receive request parameters

Guess you like

Origin blog.csdn.net/qq_61313896/article/details/128862195