spring-projects / spring-boot

Spring Boot helps you to create Spring-powered, production-grade applications and services with absolute minimum fuss.
https://spring.io/projects/spring-boot
Apache License 2.0
75.46k stars 40.76k forks source link

Provide a configuration option to load properties config data files as UTF-8 #28663

Open lfz757077613 opened 3 years ago

lfz757077613 commented 3 years ago

It works well when I set my properties in application.yml directly But when I set these properties in additional file imported by "spring.config.import" like below, its charset is wrong Is there some options to set charset for "spring.config.import"? Otherwise I have to use @PropertySource to set charset, but it do not support yml file image

wilkinsona commented 3 years ago

What do you mean by wrong? Which charset did you expect to be used and which charset was actually used? Can you provide a minimal sample that reproduces the problem so that we can see exactly what you mean? You can share one with us by zipping it up and attaching it to this issue or by pushing it to a separate repository on GitHub.

lfz757077613 commented 3 years ago

What do you mean by wrong? Which charset did you expect to be used and which charset was actually used? Can you provide a minimal sample that reproduces the problem so that we can see exactly what you mean? You can share one with us by zipping it up and attaching it to this issue or by pushing it to a separate repository on GitHub.

you can try this demo project, all of files are utf8. I have tried to debug to locate the cause, but its a little complex to find where run the decoding code.. If I use @PropertySource(value = {"classpath:other.properties"}, encoding = "UTF-8") instead of "spring.config.import", it works fine

image

springtest.zip

wilkinsona commented 3 years ago

Java's default encoding for properties files is ISO-8859-1 and we deliberately align with this (see https://github.com/spring-projects/spring-boot/issues/5361 and https://github.com/spring-projects/spring-boot/pull/10565). As recommended in Properties' javadoc you should use Unicode escapes for non-Latin1 characters. YAML uses UTF-8 by default which is why the value of static-config.right is correct.

We can consider offering a setting to load .properties files using UTF-8 but it will be somewhat difficult to set as it creates something of chicken and egg problem.

lfz757077613 commented 3 years ago

Thanks! I understand, this case confused me a long time. For now, I think "yml" is more convenient than "properties" in springboot project. Perhaps "spring.config.import" should provide a prefix for encoding such like "optional" prefix. Thanks again.

philwebb commented 3 years ago

We'd like to see how the UTF-8 switch planned for Java 18 works out before we do anything in Spring Boot. For now, using YAML seems like the best approach.