1. 程式人生 > 實用技巧 >記錄 springboot 讀取配置檔案中文亂碼問題

記錄 springboot 讀取配置檔案中文亂碼問題

今天做一個簡訊模板讀取 ,內容包含中文,由於資訊敏感就不貼出來了,大概是由於設定編碼問題導致讀出來的內容是亂碼的

  1. 先檢查了檔案編碼格式,預設預設繼承UTF-8,沒有問題。

  1. 檢查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>();
}

  1. 修改正確配置檔案類

@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>();
}

返回也不亂碼了