【问题标题】:500 Internal Server Error - AngularJs, RestFull500 内部服务器错误 - AngularJs,RestFull
【发布时间】: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 表示服务器端的异常......所以尝试调试它。它可以是很多东西

标签: angularjs rest


【解决方案1】:

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

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

也许你可以从这个小sn-p代码中获得更多信息。

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

希望这会对你有所帮助。

【讨论】:

  • 现在我有错误:'org.springframework.dao.DuplicateKeyException:具有相同标识符值的不同对象已与会话关联'
  • 不能说这个问题。但是对于您在 stackOverflow 上提供的错误有多个问题。这可能会对您有所帮助:stackoverflow.com/questions/4194350/…
  • 我忘了添加到 RestController: 'currentUser.setAuthority(userJSON.getAuthority());'现在我有'PUT 405(不允许的方法)'
  • 接受答案,如果您认为给出的答案有帮助:)
  • 谁能帮帮我??
猜你喜欢
  • 2015-04-10
  • 2016-07-01
  • 2016-09-15
  • 2019-01-17
  • 2011-10-17
  • 2010-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多