记spring mvc传入List<Object>的一次尝试
首先,看一段异常:
org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unrecognized token 'entityList': was expecting ('true', 'false' or 'null')
标记的是实体中定义的属性值,实体如下:
@Data public class ColumnsVO { List<GridColumnInfoEntity> entityList = new ArrayList<>(); }
前端js代码:
$.ajax({
url: 'grid/add.columns',
type: "POST",
data: {
entityList: rows
},
success: function (data) {
layer.msg(data.message);
},
dataType: "json",
contentType: "application/json"
});
controller处理代码:
@RequestMapping(value = "/add", consumes = "application/json; charset=utf-8") public EasyuiResponse addColumns(@RequestBody ColumnsVO columnsVO) { System.out.println(JSON.toJSONString(columnsVO)); return new EasyuiResponse(EasyuiResponse.SUCCESS, "配置成功!"); }
之前$.ajax中data一直是使用{key:value}方式,这次也不例外,结果就报错了。。。
搜索、查找后解决方案:
$.ajax({
url: 'grid/add.columns',
type: "POST",
data: JSON.stringify({
entityList: rows
}),
success: function (data) {
layer.msg(data.message);
},
dataType: "json",
contentType: "application/json"
});
和上面比,只有一点区别,将{key:value}使用JSON.stringify({key:value})转化为String后,一切正常!
一个比较奇怪的问题,记之。。。
另:spring mvc中传入集合需要注意的点:
1、看代码:
@RequestMapping(value = "/add", consumes = "application/json; charset=utf-8")
public EasyuiResponse addColumns(@RequestBody ColumnsVO columnsVO) {
System.out.println(JSON.toJSONString(columnsVO));
return new EasyuiResponse(EasyuiResponse.SUCCESS, "配置成功!");
}
标记部分需要注意!
2、则是上文讨论部分,注意之!