Spring Boot default properties encoding change?

我的未来我决定 提交于 2019-12-01 09:15:40

Apparently properties loaded by Spring Boot's ConfigFileApplicationListener are encoded in ISO 8859-1 character encoding, which is by design and according to format specification.

On the other hand, the .yaml format supports UTF-8 out of the box. A simple extension change fixes the problem for me.

@JockX suggestion works perfectly. Also, the conversion from property to yaml is quite straightforward. This:

spring.main.web_environment=false
email.subject.text=Here goes your subject
email.from.name=From Me
email.from.address=me@here.com
email.replyTo.name=To Him
email.replyTo.address=to@him.com

Would become:

spring:
  main:
    web_environment: false
email:
  subject:
    text: Here goes your subject
  from:
    name: From Me
    address: me@here.com
  replyTo:
    name: To Him
    address: to@him.com

Another approach would be Instead of renaming the complete file from .properties to .yml you can pick the props which need UTF-8 support and move them to .yml file. This way you need not rewrite ur .properties file.

I advice this because if you have props like

my.string.format= %s-hello-%s

This breaks in .yml files. You would have to write them as

my.string.format: |
   %s-hello-%s

Which then leads to adding a new line in the property valye my.string.format when read in the Java code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!