报错:
后台使用@RequestBody接收集合参数
后来发现问题:前端传递参数时,没有指定 contentType : 'application/json;charset=utf-8' ,当没有指定是默认的请求类型是:“application/x-www-form-urlencoded;charset=UTF-8”,传递的是json对象而后台使用@RequestBody注解进行接收就导致报的上述错误,@RequestBody注解只能接收Json字符串数据
如果上述还是不行,让前端传递json的字符串,后台用字符串接收,再转成json的
如下:
总结:
Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctype (MIME编码)是application/json,因此发送POST请求时需要设置请求报文头信息,否则Spring MVC在解析集合请求参数时不会自动的转换成JSON数据再解析成相应的集合
备注:
前端请求传Json对象则后端使用@RequestParam
前端请求传Json对象的字符串则后端使用@RequestBody
示例:
1:接收List<String>集合参数:
//-------前端
var idList = new Array();
idList.push(“1”);
idList.push(“2”);
idList.push(“3”);
var isBatch = false;
$.ajax({
type: "POST",
url: "<%=path%>/catalog.do?fn=deleteCatalogSchemes",
dataType: 'json',
data: {"idList":idList,"isBatch":isBatch},
success: function(data){
…
},
error: function(res){
…
}
});
//-----后台
@RequestMapping(value = "save")
@ResponseBody
public AjaxJson save(@RequestParam("idList[]") List<String> idList,Boolean isBatch) {
}
}
2:接收List<User>、User[]集合参数:
//-----前端
var userList = new Array();
userList.push({name: "李四",pwd: "123"});
userList.push({name: "张三",pwd: "332"});
$.ajax({
type: "POST",
url: "<%=path%>/catalog.do?fn=saveUsers",
data: JSON.stringify(userList),//将对象序列化成JSON字符串
dataType:"json",
contentType : 'application/json;charset=utf-8', //设置请求头信息
success: function(data){
},
error: function(res){
}
});
//-----后台
1、User实体类:
public class User {
private String name;
private String pwd;
//省略getter/setter
}
@RequestMapping(value= "saveUsers")
@ResponseBody
public AjaxJson saveUsers(@RequestBody List<User> userList) {
}
如果想要接收User[]数组,只需要把saveUsers的参数类型改为@RequestBody User[] userArray就行了。
3:接收List<Map<String,Object>>集合参数:
//------前端
var userList = new Array();
userList.push({name: "李四",pwd: "123"});
userList.push({name: "张三",pwd: "332"});
$.ajax({
type: "POST",
url: "<%=path%>/catalog.do?fn=saveUsers",
data: JSON.stringify(userList),//将对象序列化成JSON字符串
dataType:"json",
contentType : 'application/json;charset=utf-8', //设置请求头信息
success: function(data){
…
},
error: function(res){
…
}
});
//------后台
@RequestMapping(value= "saveUsers")
@ResponseBody
public AjaxJson saveUsers(@RequestBody List<Map<String,Object>> listMap) {
…
}
4:接收User(bean里面包含List)集合参数:
//------前端
var customerArray = new Array();
customerArray.push({name: "李四",pwd: "123"});
customerArray.push({name: "张三",pwd: "332"});
var user = {};
user.name = "李刚";
user.pwd = "888";
user. customers = customerArray;
$.ajax({
type: "POST",
url: "<%=path%>/catalog.do?fn=saveUsers",
data: JSON.stringify(user),//将对象序列化成JSON字符串
dataType:"json",
contentType : 'application/json;charset=utf-8', //设置请求头信息
success: function(data){
…
},
error: function(res){
…
}
});
//------后台
User实体类:
public class User {
private String name;
private String pwd;
private List<User> customers;//属于用户的客户群
//省略getter/setter
}
@RequestMapping(value= "saveUsers")
@ResponseBody
public AjaxJson saveUsers(@RequestBody User user) {
List<User> customers = user.getCustomers();
…
}