Edit RequestBody before crudRepository.save, how?

I want to add a unique string to the name provided in the JSON. I have spent some time googling and can’t find an answer. It seems a straightforward task, what I want is:

 @PostMapping("/users")
 public User createNewUser(@RequestBody User newUser){
 private String uniqueID = UUID.randomUUID().toString();
 User editedUser = newUser;

 //ADD MY UUID TO "DISPLAYNAME" in RequestBody

  userRepository.save(editedUser);
  return editedUser;
 }

Are you trying to append the UUID to the name, or have it as a seperate field? If you are the one defining the User class, you can just make sure the name or uuid field has a setter. Then use that setter to add the UUID into the object.

Is this what you’re asking?

Sorry for not being more specific. I ended up with this solution:

 @PostMapping("/users")
 public User createNewUser(@RequestBody User newUser){
 String uniqueID = UUID.randomUUID().toString().substring(0,4);
 newUser.setDisplayName(newUser.getDisplayName()+uniqueID);
 return userRepository.save(newUser);

so, easy as pie, I was to quick to jump the forums instead of thinking.

I’ve finished the project by the way, will put it up here soon with link to my first GitHub projekt.

I appreciate you being on here and reading my/our questions! Have a good one.

1 Like