Java 错误:缺少所需的请求正文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42736867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bug: Required request body is missing
提问by Vu Minh Vuong
I'm trying spring framework. I have RestController and function:
我正在尝试 spring 框架。我有 RestController 和功能:
@RequestMapping(value="/changePass", method=RequestMethod.POST)
public Message changePassword(@RequestBody String id, @RequestBody String oldPass,
@RequestBody String newPass){
int index = Integer.parseInt(id);
System.out.println(id+" "+oldPass+" "+newPass);
return userService.changePassword(index, oldPass, newPass);
}
and code angularJS
和代码angularJS
$scope.changePass = function(){//changePass
$scope.data = {
id: $scope.userId,
oldPass:$scope.currentPassword,
newPass:$scope.newPassword
}
$http.post("http://localhost:8080/user/changePass/", $scope.data).
success(function(data, status, headers, config){
if(date.state){
$scope.msg="Change password seccussful!";
} else {
$scope.msg=date.msg;
}
})
.error(function(data, status, headers, config){
$scope.msg="TOO FAIL";
});
}
and when i run it.
当我运行它时。
Error Message :
错误信息 :
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public com.csc.mfs.messages.Message com.csc.mfs.controller.UserController.changePassword(java.lang.String,java.lang.String,java.lang.String)
Help me fix it, pls...
帮我修一下,请...
采纳答案by mhasan
Issue is in this code.
问题出在这段代码中。
@RequestBody String id, @RequestBody String oldPass,
@RequestBody String newPass
You cannot have multiple
@RequestBody
in same method,as it can bind to a single object only (the body can be consumed only once).
@RequestBody
同一方法中不能有多个,因为它只能绑定到单个对象(主体只能被消耗一次)。
APPROACH 1:
方法 1:
Remedy to that issue create one object that will capture all the relevant data, and than create the objects you have in the arguments.
解决该问题的方法是创建一个将捕获所有相关数据的对象,然后创建您在参数中拥有的对象。
One way for you is to have them all embedded in a single JSON as below
一种方法是将它们全部嵌入到单个 JSON 中,如下所示
{id:"123", oldPass:"abc", newPass:"xyz"}
And have your controller as single parameter as below
并将您的控制器作为单个参数,如下所示
public Message changePassword(@RequestBody String jsonStr){
JSONObject jObject = new JSONObject(jsonStr);
.......
}
APPROACH 2:
方法 2:
Create a custom implementation of your own for ArgumentResolver
创建您自己的自定义实现 ArgumentResolver
回答by bhavin
You can't have request body for the GET method. If you want to pass username and password as part of request body then change RequestMethod type to POST/PUT.
您不能有 GET 方法的请求正文。如果您想将用户名和密码作为请求正文的一部分传递,请将 RequestMethod 类型更改为 POST/PUT。
If you want to use GET only then you will have to pass username and password as either path variables or request/query parameters - which is not best practice.
如果您只想使用 GET,那么您必须将用户名和密码作为路径变量或请求/查询参数传递 - 这不是最佳实践。
I would recommend changing RequestMethod and pass username & password as request body.
我建议更改 RequestMethod 并将用户名和密码作为请求正文传递。