当我使用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);
}
);
},
答案 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);
}
);
},