问题:@Requestbody 用的时候遇到400和415错误,因为请求格式不对。
@RequestBody
@RequestBody能把简单json结构参数转换成实体类,如下代码:
@RequestMapping(value = "/testUser", method = RequestMethod.POST) public String testUser(@RequestBody User user){ System.out.print(user.getAge()); return "success"; }
参数为:
{ "id":1,"user":"pkxutao","name":"name","age":18}
注意请求的content type要设置为application/json
$.ajax({ type:"POST", url : "adjectfkController/exportTemplate", cache:false, async:true, dataType:"json", contentType : 'application/json;charset=utf-8', //设置请求头信息 data:jsonstr, success : function(result) { console.log(result); }, error : function(result) { console.log("错误-----------------------------"); } });
学习链接:https://blog.csdn.net/qq_27093465/article/details/50519444