Spring接收List泛型对象参数

 前端

参数必须是数组累着

传入data时,转换 JSON.stringify(testList)

headers必须有属性

Accept': 'application/json',

'Content-Type': 'application/json'

 

var testList=[];
var user={};
user.id=1;
user.name='jack';
testList.push(user);
var user2={};
user2.id=2;
user2.name='tom';
testList.push(user2);
$.ajax({
    // headers必须添加,否则会报415错误
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
  type: 'POST',
  dataType: "json", //表示返回值类型,不必须
  data: JSON.stringify(testList),
  url: '/test/postList',
  success: function(){
      alert('success');
  }
  
});

 后台

参数前面必须有注解 @RequestBody

@RequestMapping("/postList")
    @ResponseBody
    public String postList(@RequestBody List<TestL> testL){
        System.out.println(testL);
        return null;
    
    }
public class TestL {
    private Integer id;
    private String name;
    
    public Integer getId() {
        return id;
    }
    
    public void setId(Integer id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
}
发布了164 篇原创文章 · 获赞 38 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/corleone_4ever/article/details/104630657