Geha
Geha

Reputation: 1475

Spring + Angularjs can't change post to put

When I use post method it works fine, but when I try to change it to put server do not recieve object.

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);
                        }
                    );

            },

How can I change it to put method?

Using 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);
                        }
                    );

            },

Upvotes: 3

Views: 51

Answers (1)

Luka Jacobowitz
Luka Jacobowitz

Reputation: 23532

You need to include the id in the URL. Otherwise your route won't match /user/{user}. It should look something like this:

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);
            }
      );

},

Upvotes: 1

Related Questions