1
am having the following questions when saving a user in an api using spring boot.
1)The best way to check if the email already exists is this?
2)in case I am sending the id of the user that has just been saved, but it is not being sent as json, just sending the value. How do I send as json? Obs: if I send the whole user it goes in json format
3)how to capture an 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);
}
Thank you very much, and thank you for presenting various ways to treat the error, I will study all of them
– Davi Resio