Spring + Angularjs不能改变帖子

时间:2016-03-24 20:15:37

标签: java angularjs spring

当我使用post方法时它工作正常,但是当我尝试将其更改为放置服务器时不接收对象。

UserController.java:

@RequestMapping(value = "/user/{user}", method = RequestMethod.POST)
    public ResponseEntity<User> updateUser(@ModelAttribute User user) {
        userService.updateRow(user);
        return new ResponseEntity<User>(user, HttpStatus.OK);
    }

user_service.js:

updateUser: function(user, id){

                    return $http.post('http://localhost:8080/user/', user)
                        .then(
                        function (response) {
                            return response.data;
                        },
                        function (errResponse) {
                            console.error('Error while updating user');
                            return $q.reject(errResponse);
                        }
                    );

            },

如何将其更改为put方法?

使用PUT:

UserController.java:

 @RequestMapping(value = "/user/{user}", method = RequestMethod.PUT)
    public ResponseEntity<User> updateUser(@RequestBody User user) {
        userService.updateRow(user);
        return new ResponseEntity<User>(user, HttpStatus.OK);
    }

user_service.js:

  updateUser: function(user, id){
                    return $http.put('http://localhost:8080/user/', user)
                        .then(
                        function (response) {
                            return response.data;
                        },
                        function (errResponse) {
                            console.error('Error while updating user');
                            return $q.reject(errResponse);
                        }
                    );

            },

1 个答案:

答案 0 :(得分:1)

您需要在网址中包含ID。否则,您的路线不会与/user/{user}匹配。看起来应该是这样的:

updateUser: function(user, id){
      return $http.put('http://localhost:8080/user/' + id, user)
        .then(
            function (response) {
                return response.data;
            },
            function (errResponse) {
                console.error('Error while updating user');
                return $q.reject(errResponse);
            }
      );

},