您的位置:首页 > 编程语言 > Java开发

springboot或springmvc传递复杂参数

2018-10-24 11:02 871 查看

本文使用的框架为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
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: