kucw.github.io
kucw.github.io copied to clipboard
SpringBoot - 解決 application.properties 中文亂碼
SpringBoot - 解決 application.properties 中文亂碼
介紹如何解決 application.properties 中文亂碼的問題
https://kucw.github.io/blog/2021/7/spring-chinese-properties/
使用 Spring Initialzr 產生的套件, 預設 application.properties 的 File Encoding 為 ISO-8859-1, 可以試試改為 UTF-8. (IntelliJ 右下角會有顯示 File Encoding)
@chanalin1229 改 IntelliJ 本身的 File Encoding 只能在 IntelliJ 內運行 Spring Boot 時生效,如果上線是使用 maven 來 build + deploy 的話,仍然會遇到這個問題
@kucw
- 變更 OS 語系
- maven build 指令加 -Dfile.encoding
- pom.xml 檔案中的 maven-compiler-plugin 都試過了嗎? 之前是有在 IntelliJ 測試玩過 application.properties 加入中文字當設定值, 在正式環境是沒試過, 畢竟正式環境的設定值大多是來自資料庫取值, 如果一定要在 properties 寫中文, 我會試一下上面三種方法
@chanalin1229 考慮到之後部署到線上的問題,只有 2, 3 有機會可以,不過我也沒有實際試過就是