错误:缺少所需的请求正文
我正在尝试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);
    }
和代码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";
        });
    }
当我运行它。
错误信息 :
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)请帮我修复它,请…
回答:
此代码中有问题。
@RequestBody String id, @RequestBody String oldPass,                                                         @RequestBody String newPass
您不能
@RequestBody在同一方法中使用多个对象,因为它只能绑定到一个对象(主体只能使用一次)。
解决该问题的方法是创建一个将捕获所有相关数据的对象,然后创建您在参数中具有的对象。
一种方法是将它们全部嵌入到单个JSON中,如下所示
{id:"123", oldPass:"abc", newPass:"xyz"}并将控制器作为单个参数,如下所示
 public Message changePassword(@RequestBody String jsonStr){        JSONObject jObject = new JSONObject(jsonStr);
.......
 }
创建自己的自定义实现 ArgumentResolver
以上是 错误:缺少所需的请求正文 的全部内容, 来源链接: utcz.com/qa/425914.html








