Springboot versão 2.1.4 Classe responsável pela personalização do retorno
package br.com.alura.config.validacao;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpStatus;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class ErroDeValidacaoHandler {
@Autowired
private MessageSource messageSource;
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public List<ErroDeFormularioDto> handle(MethodArgumentNotValidException exception) {
List<ErroDeFormularioDto> dto = new ArrayList<>();
List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
fieldErrors.forEach(e -> {
String mensagem = messageSource.getMessage(e, LocaleContextHolder.getLocale());
ErroDeFormularioDto erro = new ErroDeFormularioDto(e.getField(), mensagem);
dto.add(erro);
});
return dto;
}
}
Classe DTO para retorno personalizado
package br.com.alura.config.validacao;
public class ErroDeFormularioDto {
private String campo;
private String erro;
public ErroDeFormularioDto(String campo, String erro) {
this.campo = campo;
this.erro = erro;
}
public String getCampo() {
return campo;
}
public String getErro() {
return erro;
}
}