記錄 springboot 讀取配置檔案中文亂碼問題
阿新 • • 發佈:2020-12-07
今天做一個簡訊模板讀取 ,內容包含中文,由於資訊敏感就不貼出來了,大概是由於設定編碼問題導致讀出來的內容是亂碼的
-
先檢查了檔案編碼格式,預設預設繼承UTF-8,沒有問題。
-
檢查springboot 讀取配置檔案類,之前亂碼是這樣的。
@Data @Configuration @PropertySource(value = "classpath:messagesTemplate.txt") //@PropertySource(value = "classpath:messagesTemplate.txt", ignoreResourceNotFound = true, encoding = "UTF-8") @ConfigurationProperties(prefix = "custer.msg") public class CusterMessageTemplate { private List<String> temp = new ArrayList<String>(); }
-
修改正確配置檔案類
@Data @Configuration //@PropertySource(value = "classpath:messagesTemplate.txt") @PropertySource(value = "classpath:messagesTemplate.txt", ignoreResourceNotFound = true, encoding = "UTF-8") @ConfigurationProperties(prefix = "custer.msg") public class CusterMessageTemplate { private List<String> temp = new ArrayList<String>(); }