隐蔽的坑,Spring Boot application.properties 的编码格式

默认情况下,Spring Boot application.properties 是以 iso-8859-1 的格式编码的。
不是UTF-8
不是UTF-8
不是UTF-8
不是ISO-8859-1 编码的字符,都是以 unicode escapes 的形式存在的,比如:

personName=\u4F59\u5E38\u6D77

用notepad++ 或者 Vim 打开 application.properties 看到的就是这样的。


image.png

根据 StackOverFlow 上的说法,想在配置文件里使用utf-8,可以使用 YAML 做配置文件。

这个帖子的下面,有人给出了一个补充:

I've faced with the same problem. In Spring Boot there are 2 PropertySourceLoader which are used to load properties in application:

PropertiesPropertySourceLoader - supports UTF-8 only when load from XML
YamlPropertySourceLoader - supports UTF-8, but you have to change configuration format to use it

YamlPropertySourceLoader 支持UTF-8

但是,对于已经使用了 application.properties 的项目,这样很不方便编辑了。

搜了一下,有人也给出了解决方案

  1. IntelliJ idea的话:
    setting-> 搜索 file encoding 然后都选择utf-8,勾上transparent,重新打开配置文件


    image.png
  2. eclipse 编辑器下
    Alt + Enter,Text file encoding ,修改默认的编码方式为utf-8

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容