I have the following doubts when saving a user to an api using spring boot.
1) Is the best way to check if email already exists?
2) In case I am sending the id of the user just saved, but it is not being sent as json, it is only sending the value. How do I submit as json? obs: if I send the entire user it goes in json format
3) how to catch a possible error when saving the user? so I can treat it or send an error status
@PostMapping
public ResponseEntity<?> novoUsuario(@RequestBody Usuario usuario){
//Se o email já existir eu retorno um status de erro
if(usuarioRepository.findByEmail(usuario.getEmail()) != null) {
return new ResponseEntity<>(HttpStatus.PRECONDITION_FAILED);
}
usuarioRepository.save(usuario);
return new ResponseEntity<>(usuario.getId(),HttpStatus.OK);
}