【发布时间】:2016-01-01 23:24:35
【问题描述】:
我有问题,因为当我尝试更新用户时出现 500 内部服务器错误。
RestController
@ResponseBody
@RequestMapping(value = {"/user/update"}, method = RequestMethod.POST)
public ResponseEntity<User> updateUser(@PathVariable("id_user") Long id_user, @RequestBody User userJSON) {
User currentUser = userService.findUser(userJSON.getId());
///.........../////////
return new ResponseEntity<User>(currentUser, HttpStatus.OK);
}
路由
.when('/user/update/:id_user', {
title: 'update',
templateUrl: 'views/user.html',
controller: 'UserEditController'
服务
angular.module('app.services').factory('User', function ($resource) {
return $resource('user/:id_user', {id_user: '@_id_user'}, {
//....//
update: {
method: 'POST',
url: '/user/update/:id_user'
}
});
});
控制器
.controller('UserEditController', function($scope, $location, $routeParams, User) {
$scope.updateUser = function() {
$scope.user.$update(function() {
$location.path('/user');
});
};
$scope.loadUser = function() {
$scope.user = User.get({ id_user: $routeParams.id_user });
};
$scope.loadUser();
});
这是我的编辑用户按钮
<div class="col-sm-12 controls">
<a class="btn btn-danger" href="/user/update/{{currentUser.id}}" ng-click="updateUser()">Save</a>
</div>
用户服务实现
public void updateUser(User user) {
User entity = userDao.findUser(user.getId());
if (entity != null) {
entity.setLogin(user.getLogin());
entity.setPassword(user.getPassword());
entity.setEmail(user.getEmail());
entity.setAuthority(user.getAuthority());}}
抽象道:
public void save(T entity) {
getSession().saveOrUpdate(entity);}
public void merge(T entity) {
getSession().merge(entity);}
UserDaoImpl:
public void saveUser(User user) {
if (user.getId() != null){
merge(user);
}else {
save(user);
}
}
用户:
@Entity
@Table(name = "USERS")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class User implements Serializable {
private static final long serialVersionUID = -935756135185747853L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false, unique = true)
private String login;
private String password;
private String email;
private Boolean enabled = true;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<Authority> authority = new HashSet<Authority>();
//....getter and setter...../
请求 JSON:
authority: [{id: 1, authority: "USER", user: 1}]
email: "a"
enabled: true
id: 1
login: "add"
password: "add"
我会很感激你的帮助。
【问题讨论】:
-
500 表示服务器端的异常......所以尝试调试它。它可以是很多东西