引言
在数据测试的过程中发现,同一个Controller同一个Mapping,使用前端页面发送ajax请求成功接收到数据,使用postman的x-www-form-urlencoded发送请求成功接收到数据,但是使用postman的raw发送请求的和restfultool发送JSON格式的请求都失败了(传入的对象为空)。
在参数前加上注解@RequestBody(@RequestBody User user)postman-raw和restfultool工具发送请求成功了,但是ajax和postman的x-www-form-urlencoded却又失败了。
本文将解释记录说明@RequestBody、@RequestParam注解与ajax、postman、restfultool发送的请求的成功与失败的原因,以及请求的发送和@RequestBody、@RequestParam两个注解的使用。
controller:
@PutMapping
public ResultData updateUser(User user){
System.out.println("要更新的对象是"+user);
return new ResultData(888,"成功",userService.updateById(user));
}
前端ajax:成功
var user = new Object();
user.username = $("#username").val();
user.password = $("#password").val();
user.id = $("#uid").val();
user._method = "put";
$.ajax({
url:"/users",
data:user,
dataType:"json",
type:"post",
success:function (res){
if (res.code==888){
alert("修改成功!");
location.href = "/html/user_list.html";
}else {
alert("修改失败!");
}
}
});
Postman——www-form-urlencoded:成功
Postman——raw JSON:失败
RestfulTool:失败
原因分析与解决办法
1.前端请求传Json对象则后端使用@RequestParam
2.前端请求传Json对象的字符串则后端使用@RequestBody。
在postman-raw和restfultool发送的都是json对象需要使用@RequestBody
在postman-www-form-urlencoded发送的是json字符串需要使用@RequestParam(参数名一样可以不加)
使用ajax两种都可以发送
ajax发送json对象:后端使用@RequestParam(参数名一样可以不加)
var user = new Object();
user.username = $("#username").val();
user.password = $("#password").val();
user.id = $("#uid").val();
user._method = "put";
$.ajax({
url:"/users",
data:user,
dataType:"json",
type:"post",
success:function (res){
if (res.code==888){
alert("修改成功!");
location.href = "/html/user_list.html";
}else {
alert("修改失败!");
}
}
});
ajax发送json字符串:后端使用@RequestBody
var user = new Object();
user.username = $("#username").val();
user.password = $("#password").val();
user.id = $("#uid").val();
// user._method = "put";
$.ajax({
url:"/users",
data:JSON.stringify(user),
dataType:"json",
contentType: "application/json;charset=utf-8",
type:"put",
success:function (res){
if (res.code==888){
alert("修改成功!");
location.href = "/html/user_list.html";
}else {
alert("修改失败!");
}
}
});
如果发送的json和应该使用的注解不一致(比如前端发送JSON对象,后端却使用@RequestBody、前端发送JSON字符串,后端使用@RequestParam)可能会抛出异常Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
问题点1:
如果Content-Type设置为“application/x-www-form-urlencoded;charset=UTF-8”无论是POST请求还是GET请求都是可以通过这种方式成功获取参数,但是如果前端POST请求中的body是Json对象的话,会报上述错误。
请求中传JSON时设置的Content-Type 如果是application/json或者text/json时,JAVA中request.getParameter("")怎么也接收不到数据。这是因为,Tomcat的HttpServletRequest类的实现类为org.apache.catalina.connector.Request(实际上是org.apache.coyote.Request)。
问题点2:
当前端请求的Content-Type是Json时,可以用@RequestBody这个注解来解决。@RequestParam 底层是通过request.getParameter方式获得参数的,换句话说,@RequestParam 和request.getParameter是同一回事。因为使用request.getParameter()方式获取参数,可以处理get 方式中queryString的值,也可以处理post方式中 body data的值。所以,@RequestParam可以处理get 方式中queryString的值,也可以处理post方式中 body data的值。@RequestParam用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST。
@RequestBody接受的是一个json对象的字符串,而不是Json对象,在请求时往往都是Json对象,用JSON.stringify(data)的方式就能将对象变成json字符串。
参考: