I have been trying to develop a Spring boot application for caching using Redis cache. My application works for all methods except the update method. Here is the code I wrote
@RestController
@RequestMapping(value = "users/")
public class UserController {
@Autowired
UserService userService;
@Autowired
UserRepository userRepository;
private static final Logger log = LoggerFactory.getLogger(UserController.class);
@RequestMapping(value = "add/", method = RequestMethod.POST)
public void saveUser(@RequestBody User user){
log.info("Saving User information to the database...");
userService.saveUser(user);
}
@Cacheable(value = "user-get" , key = "#id")
@RequestMapping(value = "get/{id}/", method = RequestMethod.GET)
public User getUserById(@PathVariable Long id){
return userService.getUserById(id);
}
@Cacheable(value = "user-get-all")
@RequestMapping(value = "get/all/", method = RequestMethod.GET)
public List<User> getAllUser(){
return userService.getAllUsers();
}
@CacheEvict(value = "user-delete", key = "#id")
@RequestMapping(value = "delete/{id}", method = RequestMethod.DELETE)
public void deleteUser(@PathVariable Long id){
userService.deleteUser(id);
}
@CachePut(value = "user-update", key = "#user.id")
@RequestMapping(value = "update/{id}/", method = RequestMethod.POST)
public User updateUser(@PathVariable Long id, @RequestBody User user){
User newuser = new User();
newuser = userRepository.findById(id).get();
newuser.setUserName(user.getUserName());
newuser.setAge(user.getAge());
return userService.saveUser(newuser);
}
}
Help me to fix the issue