Message Source with Spring Boot

4 weeks ago 18
ARTICLE AD BOX

I am trying to use MessageSource with my Spring application for message constant. I tried everything but my message is not shown in Swagger.

/src/main/resources/message_en.properties

This is my message in constant file

ADMIN_USER_NOT_FOUND=Admin user not found

I also put this in application properties file

spring.messages.basename=message spring.messages.encoding=UTF-8

Now

@Component public class MessageUtil { private final MessageSource messageSource; public MessageUtil(MessageSource messageSource) { this.messageSource = messageSource; } public String getMessage(String message) { return messageSource.getMessage(message, null, LocaleContextHolder.getLocale()); } public String getMessageWithArgs(String message, Object[] args) { return messageSource.getMessage( message, args, LocaleContextHolder.getLocale() ); } }

This is how I define message utils and I am using like this

@Autowired MessageUtil messageUtil throw new NotFoundException(messageUtil.getMessage("ADMIN_USER_NOT_FOUND"));

But I am getting below error:

org.springframework.context.NoSuchMessageException: No message found under code 'ADMIN_USER_NOT_FOUND' for locale 'en_US'.

Why is this happening?

Read Entire Article