spring boot 中文亂碼的終結解決方案
阿新 • • 發佈:2020-12-03
亂碼問題
spring boot 中亂碼有兩種情況,一種配置檔案中引入的變數亂碼,一種是程式處理中文的亂碼。
具體意思看程式碼, 這裡 title 是properties配置檔案 引入的中文,初次使用一般是亂碼。
這裡“我” 是程式碼寫的, 引數 name 是請求時傳入中文。 預設這兩個不會亂碼。
@Value("${com.neo.title}") private String title; @GetMapping("/") public Map<String,String> index(@RequestParam(name = "name", defaultValue="world") String para) { Map<String,String> ret = new HashMap<>(); ret.put("title","hello"+para+title); ret.put("name","我"); return ret; }
配置檔案亂碼解決
參照網上方案一般能解決了,方案如下:
開啟Settings>Editor>File Encodings ,
將Properties Files (*.properties)
下的Default encoding for properties files
設定為UTF-8
,將Transparent native-to-ascii conversion
前的勾選上。
如果還不能解決,嘗試在配置檔案回車,修改等改動檔案,或者刪除檔案重新新建。
如果還不能解決,請引用文章說的方式,用yml 格式的配置檔案。
程式內容亂碼,一般在配置檔案裡配置下面引數可以解決。
spring.http.encoding.force=true spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true server.tomcat.uri-encoding=UTF-8
參考:
https://www.huangyunkun.com/2016/12/08/spring-boot-properties-encoding-issue/
https://www.cnblogs.com/tingtingzhou/p/10438814.html
https://www.cnblogs.com/telwanggs/p/10779833.html