zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    第一天开通博客,记录自己在编程道路上的点点滴滴
    一个非常棒的jQuery 评分插件--好东西要分享
    jquery.easing的使用
    SEO优化
    nodejs(三)下之mangoDB
    nodejs(三)上之express
    nodejs(二)
    nodejs(一)
    angular(二)
    angular(一)
  • 原文地址:https://www.cnblogs.com/swing07/p/14082737.html
Copyright © 2011-2022 走看看