1、以实体类方式接收
@Controller @RequestMapping("/user") public class UserAction { @RequestMapping(value = "/add") @ResponseBody public String add(@RequestBody User u) { System.out.println(u+">>>>>"); return "success"; } }
2、以Map接收
@Controller @RequestMapping("/user") public class UserAction { @RequestMapping(value = "/add") @ResponseBody public String updateAttr(@RequestBody Map<String, Object> user) { //打印user return "success"; } }
总结:
第一种方式适合于请求的json数据能在后端中找到对应的bean,第二种方式(Map<String, Object>)更通用,可以接收的类型很多,用于没有对应的bean的只是临时取值的情况。
请求端:
$.ajax({ type: method, url: url, data:JSON.stringify(data), dataType: "json", contentType: "application/json;charset=utf-8", success: function(res) { resolve(res); } })
转 : https://blog.csdn.net/qq_22771739/article/details/82284858