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?

Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
  • 4
    This question is answered here, check @merz's answer: https://stackoverflow.com/questions/37436927/utf-8-encoding-of-application-properties-attributes-in-spring-boot – z atef Aug 27 '20 at 04:22
  • 1
    Also, spring boot 2.4 has alot of new changes to how config props are loading in stuff, worth knowing. https://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4 You can for example, have have mor ethan one props file..etc – z atef Aug 27 '20 at 04:25

3 Answers3

5

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"
Oliver
  • 332
  • 1
  • 11
  • 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. – Garret Wilson Aug 31 '20 at 13:48
  • If you really want to go deeper into this you can checkout the [PropertiesPropertySourceLoader.java](https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/PropertiesPropertySourceLoader.java#L63) source code. This loads the default application.properties within Spring Boot. – Oliver Sep 01 '20 at 16:41
  • 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? – Garret Wilson Sep 02 '20 at 14:30
  • 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? – Garret Wilson Sep 03 '20 at 03:05
  • 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: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedPropertiesLoader.java#L184 – Oliver Sep 03 '20 at 17:20
  • @GarretWilson fit this answer to you? – Oliver Feb 06 '21 at 09:25
1

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

SOUMEN K
  • 448
  • 1
  • 3
  • 6
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;

`