不考虑再添加一个封装类,后台代码就只能这样,唯一的改变可以更改入参的位置,还有入参上的注解,和方法上的注解,前台应该怎么写?@ResponseBody@RequestMapping(value = "add", consumes = "application/json; charset=utf-8")public String add(@RequestBody String content, @RequestBody List<Tag> param) {前台:$.ajax({ type:"post", url: 'add', contentType:"application/json; charset=utf-8", dataType:"json", data:JSON.stringify({ content:"nihaoahaha", param:[{id:1,name:"hello"},{id:2,name:"hello"}] }), success:function(data){ console.log(data); }})如果我只接收一个list是正常的:后台代码:@ResponseBody@RequestMapping(value = "add", consumes = "application/json; charset=utf-8")public String add(@RequestBody List<Tag> param) { System.out.println("param:" + param); return "成功";}前台代码:$.ajax({ type:"post", url: 'add', contentType:"application/json; charset=utf-8", dataType:"json", data:JSON.stringify([{id:1,name:"hello"},{id:2,name:"hello"}]), success:function(data){ console.log(data); }})正常
2 回答
喵喔喔
TA贡献1735条经验 获得超5个赞
如果是单个参数用@RequestParam注解,一个对象或者集合就用 @RequestBody,
但是不建议@RequestBody和@RequestParam一起用,
你可以把content封装到Tag对象里面加个非表字段注解@TableField(exist = false),
这样传参就只需要把参数都丢到Tag对象里,前后台都方便,就容易多了。
比如:
Public class Tag{
private String 参数1;
private String 参数2;
@TableField(exist = false)//非表字段注解,不干涉数据库操作
private String content;
getter..setter...
....
}
添加回答
举报
0/150
提交
取消