500内部服务器错误 - AngularJs,RestFull

时间:2016-01-01 23:24:36

标签: angularjs rest

我遇到问题,因为当我尝试更新用户时出现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>

UserServiceImpl

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

AbstractDao的:

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"

我将非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

很难说什么,因为我无法看到您发送的更新到服务器的请求对象。但在内部服务器端错误期间会出现500。我建议你彻底调试你的服务器端代码。

另外,因为它是更新调用,所以它不能是RequestMethod.POST,它必须是RequestMethod.PUT。

您可以从这一小段代码中获取更多信息。

@RequestMapping(method = RequestMethod.PUT, value = "/datasources/{dataSourceId}")
    @ResponseStatus(HttpStatus.OK)
    public @ResponseBody
    CloudWebServiceResponse updateDataSource(@RequestBody String dataSource, @PathVariable int dataSourceId)
            throws CloudWebServiceInvocationException {

        return dataSourceService.updateDataSource(dataSource, dataSourceId);
    }

希望这会对你有所帮助。