I am trying to read my error messages from a property file but I encounter this error message
org.springframework.context.NoSuchMessageException: No message found under code 'error.id.invalid' for locale 'en'.
I am using ResourceBundleMessageSource to set the base name
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("classpath:validations/messages.properties");
return messageSource;
}
This is my exception Handler
@Autowired
private ResourceBundleMessageSource messageSource;
@ExceptionHandler(value = {ConstraintViolationException.class})
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorMessageDTO handleResourceNotFoundException(ConstraintViolationException e) {
ErrorMessageDTO message = null;
if (e != null) {
Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
StringBuilder strBuilder = new StringBuilder();
for (ConstraintViolation<?> violation : violations) {
Locale currentLocale = LocaleContextHolder.getLocale();
String msg = messageSource.getMessage(violation.getMessage(), null, currentLocale);
strBuilder.append(msg).append(", ");
}
message = new ErrorMessageDTO(MessageType.ERROR, strBuilder.toString(), BAD_REQUEST_ACTION, MODEL_INVALID_CODE);
try {
slf4jLogger.error(mapper.writeValueAsString(message));
} catch (JsonProcessingException ex) {
slf4jLogger.error(ex.getMessage());
}
}
return message;
}
This is the directory structure of my project
src
main
resources
validations
messages_en_US.properties
Can someone please help me figure out what have I am I missing or have done wrong?
I know there are many questions related to this error but none of the solutions worked for me. Any help is appreciated.
Thank you in advance.