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:

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


  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:

 public User createNewUser(@RequestBody User newUser){
 String uniqueID = UUID.randomUUID().toString().substring(0,4);
 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