why does the spring boot update method create a new record?

You are not checking if user is present or not and you creating new record by calling save method. save method always insert new row if id in given object is not present in DB, in your case user is having id 0 I guess, thats why it is inserting new record. You need to fetch the User from given id and update the existing record in DB.

Create one more method in service class

        public Users getUserById(Integer id) {
             return repositoryInterface.findById(id).orElse(null);

And then..

       public Users updateUser(@PathVariable Integer id, @RequestBody Users user) {
           Users userExisting =  serveiceClass.getUserById(id);
           if(userExisting  == null){
              throw Exception("User Not Found");
          return serveiceClass.updateUserbyId(userExisting);

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top