kucw.github.io icon indicating copy to clipboard operation
kucw.github.io copied to clipboard

SpringBoot - 解決 application.properties 中文亂碼

Open utterances-bot opened this issue 4 years ago • 4 comments

SpringBoot - 解決 application.properties 中文亂碼

介紹如何解決 application.properties 中文亂碼的問題

https://kucw.github.io/blog/2021/7/spring-chinese-properties/

utterances-bot avatar Oct 14 '21 09:10 utterances-bot

使用 Spring Initialzr 產生的套件, 預設 application.properties 的 File Encoding 為 ISO-8859-1, 可以試試改為 UTF-8. (IntelliJ 右下角會有顯示 File Encoding)

chanalin1229 avatar Oct 14 '21 09:10 chanalin1229

@chanalin1229 改 IntelliJ 本身的 File Encoding 只能在 IntelliJ 內運行 Spring Boot 時生效,如果上線是使用 maven 來 build + deploy 的話,仍然會遇到這個問題

kucw avatar Oct 16 '21 19:10 kucw

@kucw

  1. 變更 OS 語系
  2. maven build 指令加 -Dfile.encoding
  3. pom.xml 檔案中的 maven-compiler-plugin 都試過了嗎? 之前是有在 IntelliJ 測試玩過 application.properties 加入中文字當設定值, 在正式環境是沒試過, 畢竟正式環境的設定值大多是來自資料庫取值, 如果一定要在 properties 寫中文, 我會試一下上面三種方法

chanalin1229 avatar Oct 18 '21 02:10 chanalin1229

@chanalin1229 考慮到之後部署到線上的問題,只有 2, 3 有機會可以,不過我也沒有實際試過就是

kucw avatar Oct 23 '21 10:10 kucw