Spring boot application and MessageSource

nicolas :

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:

  • MessageSourceAutoConfiguration
  • Creating my own bean within a @configuration file and autowiring it

Below is the MessageSourceAutoConfiguration way:

I am using spring-boot-starter-parent 1.5.7.RELEASE.

My folder structure:

enter image description here

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

nicolas :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=432475&siteId=1