Olá professor, a conversão de idioma com o LocaleContextHolder.getLocale, não funcionou.
Poderia me confirmar se as classes estão corretas?
Abaixo segue código.
Obrigado
package br.com.gmartins.resources.exceptions;
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 ResourceExceptionHandler {
@Autowired
private MessageSource messageSource;
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public List<StandardErrorDto> handler(MethodArgumentNotValidException excepetion){
List<StandardErrorDto> objDto = new ArrayList<>();
List<FieldError> fieldErrors = excepetion.getBindingResult().getFieldErrors();
fieldErrors.forEach(e -> {
String mensagem = messageSource.getMessage(e, LocaleContextHolder.getLocale());
StandardErrorDto error = new StandardErrorDto(e.getField(), mensagem, System.currentTimeMillis());
objDto.add(error);
});
return objDto;
}
}