1

I have problem because when I try update user occure 500 Internal Server Error.

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

Routing

.when('/user/update/:id_user', { 
        title: 'update',
        templateUrl: 'views/user.html',
        controller: 'UserEditController'

Service

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

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

This is my button for edit user

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

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...../

Request JSON:

authority: [{id: 1, authority: "USER", user: 1}]
email: "a"
enabled: true
id: 1
login: "add"
password: "add"

I will be grateful for your help.

Mateusz
  • 11
  • 4

1 Answers1

0

Its difficult to say anything as I am not able to see your request object that you are sending to update to the server. But 500 comes during internal sever side error. I suggest you to debug thoroughly your server side code.

Also, as it is update call, so it cannot be RequestMethod.POST, it must be RequestMethod.PUT.

May be you can get some more info from this small snippet of code.

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

Hope this will help you.

Azim
  • 1,043
  • 13
  • 27
  • Now i have error: 'org.springframework.dao.DuplicateKeyException: A different object with the same identifier value was already associated with the session' – Mateusz Jan 03 '16 at 12:08
  • Can't say anything about this issue. But there are multiple questions on the error that you have provided on stackOverflow. This may help you: http://stackoverflow.com/questions/4194350/a-different-object-with-the-same-identifier-value-was-already-associated-with-th – Azim Jan 03 '16 at 12:28
  • I forgot add to RestController: 'currentUser.setAuthority(userJSON.getAuthority());' and now i have 'PUT 405 (Method Not Allowed)' – Mateusz Jan 03 '16 at 12:39
  • Accept the answer, if you find the answer given was helpful :) – Azim Jan 03 '16 at 12:42
  • Anyone can help me?? – Mateusz Jan 03 '16 at 21:51