本文使用的框架为springboot-2.0.4.RELEASE版本
1、传递List<实体类>
@PostMapping("/test")
public DataWrapper test(@RequestBody List<Jwt> jwts){
return success(jwts);
}
这里使用的注解为@RequestBody、使用该注解、前台发送请求时的Content-Type应该为:application/json类型、否则可能会接受不到参数
let jwt = [];//声明一个数组
jwt.push({scope:'1111'});//加入数组中
jwt.push({scope:'1111'});//加入数组中
jwt.push({scope:'1111'});//加入数组中
$.ajax({
url:'xxxxx',
Content-Type:'application/json;charset=UTF-8',
data:jwt,//jwt 作为参数传递、也就是个List<jwt>类型
//其他的省略
});
2、传递list<常规类型>如String、int等类型
@PostMapping("/test")
public DataWrapper test(@RequestParam List<Integer> ids,@RequestParam type){
return success(ids);
}
这里使用@RequestParam注解、Content-Type为application/x-www-form-urlencoded即可、至于其他的我没试、以下是js代码的一个小片段
let ids = new Array();//这是数组
for (var i = 0; i < length; i++) {
ids.push(datas[i].id);
}
//这个是请求参数、请求为json类型、其中ids为数组类型、转换到后台也就是List<int>或者int[]
//剩下的发送请求等等省略
let data = {
'ids':ids,
'type':type
}