I have been trying to create a demo spring boot app with MessageSource, but i couldn't figure out what is the problem. I tried in couple of ways:
Below is the MessageSourceAutoConfiguration way:
I am using spring-boot-starter-parent 1.5.7.RELEASE.
My folder structure:
DemoApplication.java
@SpringBootApplication
public class DemoApplication
{
public static void main(String[] args)
{
SpringApplication.run(DemoApplication.class, args);
}
}
DemoController.java
@RestController
public class DemoController implements MessageSourceAware
{
private MessageSource messageSource;
@Override
public void setMessageSource(MessageSource messageSource)
{
this.messageSource = messageSource;
}
@RequestMapping("demo")
public String getLocalisedText()
{
return messageSource.getMessage("test", new Object[0], new Locale("el"));
}
}
Application.yml
spring:
messages:
basename: messages
messages.properties
test=This is a demo app!
messages_el.properties
test=This is a greek demo app!
pom.xml
<groupId>com.example.i18n.demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath />
<!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-
8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
While starting up the server, i can see that the AutoConfiguration works, but cannot find a bean:
MessageSourceAutoConfiguration matched:
- ResourceBundle found bundle URL [file:/C:/Users/{user}/Downloads/demo/demo/target/classes/messages.properties] (MessageSourceAutoConfiguration.ResourceBundleCondition)
- @ConditionalOnMissingBean (types: org.springframework.context.MessageSource; SearchStrategy: current) did not find any beans (OnBeanCondition)
I tried to also explicitly declare my own bean, but didn't work.
While invoking the endpoint i get the following error:
{
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.context.NoSuchMessageException",
"message": "No message found under code 'test' for locale 'el'.",
"path": "/demo"
}
The closest SO question i found was this one (2 years old) about a bug in spring which was fixed couple of versions ago: Spring Boot MessageSourceAutoConfiguration
MessageSource is a powerful feature available in Spring applications. This helps application developers handle various complex scenarios with writing much extra code, such as environment-specific configuration, internationalization or configurable values.
Spring Boot helps developers create applications that just run. Specifically, it lets you create standalone applications that run on their own, without relying on an external web server, by embedding a web server such as Tomcat or Netty into your app during the initialization process.
Adding Message Properties Into The Spring Boot Project In order to do that, just create two files in src/main/resources/messages folder with naming as below, api_error_messages. properties. api_response_messages.
Can you create a messages package in resources and try this Bean implementation in your configuration file:
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setCacheSeconds(10); //reload messages every 10 seconds
return messageSource;
}
Additionally, I suggest you to use @Configuration annotated configuration classes instead of xml files to be adapted perfectly to Spring Boot concept.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With