SpringBoot - UTF-8 Doesnt work in messages.properties

QoP picture QoP · Jan 23, 2015 · Viewed 31.8k times · Source

¿What's the problem?

I can't display in UTF-8 the messages I got in messages.properties.

An example

<h1 id="logo">Electrónico</h1>

this works okay but when I try to use my message source like this

<h1 id="logo" th:text="#{titulo.electronico}">Electrónico</h1>

I get "Electr�nico" instead of Electrónico

This is my configuration

application.properties

spring.messages.encoding=UTF-8
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8

pom.xml

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <start-class>demo.Demo</start-class>
    <java.version>1.7</java.version>
</properties>

Demo class

@SpringBootApplication
public class Demo {

    public static void main(String[] args) {
        SpringApplication.run(Demo.class, args);
    }
}

ServletInitializer.class

@Configuration
public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Demo.class);
    }

    @Bean
    public ServletRegistrationBean h2servletRegistration() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
        registration.addUrlMappings("/console/*");
        return registration;
    }

    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    CharacterEncodingFilter characterEncodingFilter() {
      CharacterEncodingFilter filter = new CharacterEncodingFilter();
      filter.setEncoding("UTF-8");
      filter.setForceEncoding(true);
      return filter;
    }
}

If you need more information about my configuration I can edit it. Thanks.

Answer

Artem Bilan picture Artem Bilan · Jan 23, 2015

To read any file in the UTF-8 encoding it must be created in the UTF-8 before.

Use some editor which supports encoding switching. Or create that file from IDE with encoding option for properties files. E.g. IDEA: http://blog.jetbrains.com/idea/2013/03/use-the-utf-8-luke-file-encodings-in-intellij-idea/