Professor Rodrigo,
Notei que ao deixar o construtor privado do record responsavel por transformar a lista de mensagens de erro do bean validation, o mesmo nao e instanciado e usado, isso e por conta do contexto do spring ? o container tem que ter a visao publica do construtor do record para conseguir instancia-lo e injeta-lo ?
package med.voll.api.infra;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@RestControllerAdvice
public class RestControllerExceptionHandler {
@ExceptionHandler(EntityNotFoundException.class)
public ResponseEntity handle404() {
return ResponseEntity.notFound().build();
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity handle400(MethodArgumentNotValidException exception) {
var errors = exception.getFieldErrors();
return ResponseEntity.badRequest().body(
errors.stream().map(DataNotValid::new).toList()
);
}
private record DataNotValid(String field, String message) {
public DataNotValid(FieldError error) {
this(error.getField(), error.getDefaultMessage());
}
}
}
Obrigado