0

Although Java Properties files traditionally supported only ISO-8859-1, JDK 9 and onward supports properties files encoded in UTF-8. And while only JDK 9+ supports UTF-8 with built-in default properties file reading, the same technique it uses could be done in any Java version, wrapping around the properties file to add UTF-8 support and fall back to ISO-8859-1 for backwards compatibility (as the JDK implementation does).

I'm new to Spring Boot, and I'm reading about all the nifty properties configurations it brings. Does Spring Boot support loading properties from a properties file encoded in UTF-8? And if not, where in the Spring Boot code is properties file reading consolidated, so that I can add this capability and submit a pull request?

2
  • 4
    This question is answered here, check @merz's answer: stackoverflow.com/questions/37436927/… Commented Aug 27, 2020 at 4:22
  • 1
    Also, spring boot 2.4 has alot of new changes to how config props are loading in stuff, worth knowing. spring.io/blog/2020/08/14/… You can for example, have have mor ethan one props file..etc Commented Aug 27, 2020 at 4:25

3 Answers 3

6
+50

By default Spring only support ISO-8859-1 properties. But there exist several ways to work around this:

  • Use application.yaml property file.
  • Anotate your @Configuration class with @PropertySource(value = "classpath:/custom-name-of-application.properties", encoding="UTF-8")
  • Run your App with the Java Argument to use UTF-8 files: mvn spring-boot:run -Drun.jvmArguments="-Dfile.encoding=UTF-8"
Sign up to request clarification or add additional context in comments.

6 Comments

And where in the code is properties file reading localized for the Spring Boot properties systems, so that I can add UTF-8 support to the source code? If it supports both application.properties and application.yaml, there must be a place in the code where there is logic that looks for .properties and .yaml files and reads them.
If you really want to go deeper into this you can checkout the PropertiesPropertySourceLoader.java source code. This loads the default application.properties within Spring Boot.
Do you have any official reference that "Spring only supports ISO-8859-1"? Is it in the documentation? Or can you at least point to the place in the code that restricts it to ISO-8859-1 support?
And your comment about where to change this in the code—don't you think it would be better as part of the answer (since that was part of the question), and not just a comment below it?
I don‘t think that the encoding is mentioned in the Spring Boot docs. But here in source code you can see the encoding setting: github.com/spring-projects/spring-boot/blob/master/…
|
1

@PropertySource(value = "classpath:application-${env}.properties", encoding = "UTF-8")

Comments

0

use

@PropertySource(value = "classpath:application.properties", encoding = "UTF-8")

at class level, then you can retrieve the single properties with @value, but you need to define this bean: `

@Bean public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { return new PropertySourcesPlaceholderConfigurer(); } @Value("${my.property1}") private String property1; 

`

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.