spring boot 中文亂碼的終結解決方案


亂碼問題

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM