因为最近项目有这个需求,开始按照自己的方法试了几次都不成功,最后在网上查找了半天,算是基本理清了原委,当然时间仓促,可能有不准确的地方,如果博友有更好的方法,欢迎留言交流.
直接上结论:
只能用JSON的方式传,有两种方法,不同之处在于是框架帮你转还是你自己转成对象
前提:要求前端请求头中contentType 类型 必须为 application/json
同时记得 要把js 对象转成字符串 作为参数,现在都是前后端分离,一般使用ajax,例如:
$.ajax({
url: "/sys/listUserRole",
data: JSON.stringify(list),
type: "post",
contentType: "application/json",
dataType: "json",
success: function(data) {
}
});
1.后端框架自动帮转,使用@RequestBody注解
@PostMapping(value = "saveList")
public JR saveList(@RequestBody List<SalaryMatterCellSource> cellSources) {
for (SalaryMatterCellSource salaryMatterCellSource : cellSources ){
if (salaryMatterCellSource.getIsNewRecord()) {
this.salaryMatterCellSourceService.save(salaryMatterCellSource);
} else {
SalaryMatterCellSource t = this.salaryMatterCellSourceService.get(salaryMatterCellSource.getId());
try {
BeanUtils.copy2Properties(salaryMatterCellSource, t);
this.salaryMatterCellSourceService.save(t);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return ok("数据保存成功!");
}
2.自己用String接收,然后用fastJson之类的工具转换,不再赘述
------------------------------------------------------------------------------------------