Reputation: 445
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
Upvotes: 27
Views: 127866
Reputation: 29
I've used the same but I didn't give any base names for my message source files but it worked.
@RestController
public class HelloWorld {
@Autowired
private MessageSource messageSource;
@GetMapping(path="/hello-world-internationalized")
public String helloWorldInternationalized(
//@RequestHeader(name ="Accept-Language", required= false )
)
{
//String str= "Hello-World";
return messageSource.
getMessage("good.morning.message", null, "Default Hello",
LocaleContextHolder.getLocale());
//return str;
}
}
Upvotes: 1
Reputation: 2970
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.
Upvotes: 37
Reputation: 1871
You can also configure through application.properties
spring.messages.basename=i18n.messages
Refer SpringBoot doc click here
Upvotes: 3
Reputation: 509
I had the same issue on springboot app. I have tested one of the below options:
If you prefer to modify application.properties
file then add this line spring.messages.basename=messages
where messages is the prefix of the file containing your messages. with this u don't have to setup a messagesource
bean yourself.
or
i had to give the MessageResource
bean a name and autowire it using the name given during initialization, otherwise DelegatingMessageSource
was being injected and it wasn't resolving to any message source.
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localResolver=new SessionLocaleResolver();
localResolver.setDefaultLocale(Locale.US);
return localResolver;
}
@Bean(name = "messageResourceSB")
public MessageSource messageResource() {
ResourceBundleMessageSource messageBundleResrc=new ResourceBundleMessageSource();
messageBundleResrc.setBasename("msg.message");
return messageBundleResrc;
}
}
then autowire the bean with the name u expect
@RestController
public class Internationalization {
@Autowired
@Qualifier("messageResourceSB")
MessageSource messageSource;
@GetMapping(path = "/sayHelloIntern")
public String sayHello(@RequestHeader(name="Accept-Language",required = false) Locale locale) {
return messageSource.getMessage("message.greeting", null, locale);
}
}
Upvotes: 8
Reputation: 445
The issue was my Eclipse encoding configuration, which I haven't managed to fix yet.
After debugging Spring's code (ReloadableResourceBundleMessageSource.java
) I could see my key=value
property loaded, but with 3 space characters before each character (e.g. t e s t = T h i s i s a d e m o a p p !
).
On another PC the same demo application works fine.
Upvotes: 3