'SpringBoot - UTF-8 Doesnt work in messages.properties
¿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.
Solution 1:[1]
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/
Solution 2:[2]
In my case, I forgot messageSource.setDefaultEncoding("UTF-8"), see here.
Solution 3:[3]
Hopefully this may help others. The various code solutions didn't work in my case but an editor settings did.
If you are using intellij go to file encoding and set global and project encoding to UTF-8, same goes for the default encoding for properties files and -very important- check the Transaparent native-to-ascii conversion.
If you open the file with a decent text editor you will notice that some characters are written with their UTF-8 version such as
é
->\u00E9
In my case the change didn't triggered a change for GIT so I had to make a "fake" modification to the file in order to do a commit and push.
Solution 4:[4]
At least in my case the problem was in messages.properties files. Because Eclipse (STS in my case) use by default ISO-8859-1 encoding.

So, just changing it to UTF-8 did the trick. But be careful, because during the conversion process (I did it after adding content) some characters can be replaced with symbols like diamonds, etc.
Solution 5:[5]
I prefer 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/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Artem Bilan |
| Solution 2 | zb226 |
| Solution 3 | Paizo |
| Solution 4 | lm2a |
| Solution 5 | Thomas Peters |


