In the SpringBoot project, the method of receiving array type data

In the SpringBoot project, the method of receiving array type data

1. First define a simple entity class

package cn.js.domain;

import lombok.Data;

@Data
public class Person {
    
    
    private String name;
    private int age;
}

2. The json data method passed by the front end:

1. The front end transmits ordinary json, and the back end uses objects to receive:

{
    
    
    "name":"张三",
     "age":12
}

Receive json with object:

@PostMapping("/test1")
  public String test(@RequestBody Person person){
    
    
      System.out.println("name"+person.getName());
      System.out.println("age"+ person.getAge());

      return "成功";
  }

2. The front end passes the json array:

{
    
    
    "name":["小明","小三","小四"],
     "age":12
}

Use Map to receive json array:

 @PostMapping("/test2")
 public String test2(@RequestBody Map hasaMap){
        ArrayList arrayList = new ArrayList();
        arrayList = (ArrayList) hasaMap.get("name");
        String str = (String) arrayList.get(0);
        System.out.println("arrayList"+str);
        System.out.println("name"+hasaMap.get("name"));
        System.out.println("age"+ hasaMap.get("age")); 
   return "成功";
}

3. The front-end passes the json array, and the back-end uses a collection to receive the Josn array

{
    
    
    "name":["小明","小三","小四"]
}

Use Map nested List to receive json array

    @PostMapping("/test2")
    public String test3(@RequestBody  Map<String,List > hasaMap){
        System.out.println("name"+hasaMap.get("name"));
    return "成功";
}

Guess you like

Origin blog.csdn.net/qq_45525848/article/details/129911245