1.第一种
前端以params的方式来发送数组
这个代码只是举一个例子,我用postman测试的
axios.get('url',params{ids:[1,2,3]})//拼接参数的方式来获取数据
后端接受Integer或者list都可以接受
@RequestMapping(value = "/deleteBatch", method = RequestMethod.GET)
@ResponseBody //@RequestParam("ids") List<Integer> ids
public Msg deleteBatch(@RequestParam("ids") Integer[] ids) {
// 执行批量删除操作
for (Integer id : ids) {
System.out.println(id);
}
return Msg.success();
}
结果:
如果不写responseBody的话,就会把函数名当成要跳转的页面
2.如果以请求体data的方式来传的话,上述方法就行不通了(会报415错误,或者400),把请求方式改成post不然会报错,请求方法不匹配
@RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
@ResponseBody //@RequestParam("ids") List<Integer> ids
public Msg deleteBatch(@RequestParam("ids") Integer[] ids) {
// 执行批量删除操作
for (Integer id : ids) {
System.out.println(id);
}
return Msg.success();
}
错误:
前端以data的方式来发送数组
axios.post('url',{ids:[1,2,3]})
后端以@RequestBody方式来接受前端返回来的数组
1.用map或者实体类来接受
@PostMapping("/delete/furns")
@ResponseBody
public Msg delete(@RequestBody Map<String, Integer[]> request){
System.out.println(request);
//通过从map获取键的方式来获取值
Integer [] id= request.get("ids");
//以下代码就是批处理操作了
//批量删除 批量添加.....
System.out.println(Arrays.toString(id));
Msg msg = Msg.success();
return msg;
}
postman测试:
结果: