springboot @ConfigurationProperties 中文乱码解决方案

您所在的位置:网站首页 女人怎么减肥效果最快最有效的方法 springboot @ConfigurationProperties 中文乱码解决方案

springboot @ConfigurationProperties 中文乱码解决方案

2024-06-11 03:06| 来源: 网络整理| 查看: 265

================================

©Copyright 蕃薯耀 2021-12-22

​​​​​蕃薯耀的博客_CSDN博客

一、springboot @ConfigurationProperties 中文乱码问题 org.springframework.boot spring-boot-starter-parent 2.5.7

1、application.properties文件: lqy.java.auto.id=10 lqy.java.auto.name=张三aa lqy.java.auto.map.a=a-一 lqy.java.auto.map.b=b-二 2、读取配置文件类 import java.util.Map; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "lqy.java.auto", ignoreUnknownFields = true) public class MyProperties { private Integer id; private String name; private Map map; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } } 3、加载出来,出现中文乱码 myProperties = { "name":"å¼\u00a0ä¸\u0089aa", "id":10, "map":{"a":"a-ä¸\u0080","b":"b-äº\u008c"} }

二、springboot @ConfigurationProperties 中文乱码解决方案 方案一:转成ASCII码

将中文全部转成ASCII码

详情略。

方案二:覆盖OriginTrackedPropertiesLoader

覆盖原文件:org.springframework.boot.env.OriginTrackedPropertiesLoader

将OriginTrackedPropertiesLoader所有代码复制出来,按照包路径建立自己的包和类(包名和类名都必须和原来的一致,不然不生效)

如下图所示:

然后修改自己的OriginTrackedPropertiesLoader上的编码:

原来是:

修改后是:StandardCharsets.UTF_8

CharacterReader(Resource resource) throws IOException { this.reader = new LineNumberReader( new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)); }

重启项目,发现项目中文乱码已经解决。

myProperties = {"name":"张三aa","id":10,"map":{"a":"a-一","b":"b-二"}} User u = {"name":"张三aa","id":10}

方案三:使用yml文件

使用yml文件也没有中文乱码。

application.yml:

lqy: java: auto: id: 11 name: 李四ll map: aa: map一 bb: map二

同时使用application.yml和application.properties,application.properties会覆盖application.yml

方案四:自定义配置文件

1、自定义配置文件:custom.properties

lqy.java.my.id=20 lqy.java.my.name=王王aa lqy.java.my.map.a=哈哈a lqy.java.my.map.b=哈哈b

2、配置类

必须设置编码:encoding = "UTF-8"

import java.util.Map; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @ConfigurationProperties(prefix = "lqy.java.my", ignoreUnknownFields = true) @PropertySource(encoding = "UTF-8", value = "classpath:custom.properties", ignoreResourceNotFound = true) public class Custom { private Integer id; private String name; private Map map; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } @Override public String toString() { return "Custom [id=" + id + ", name=" + name + ", map=" + map + "]"; } }

输出没乱码:

custom = Custom [id=20, name=王王aa, map={b=哈哈b, a=哈哈a}]

(时间宝贵,分享不易,捐赠回馈,^_^)

 ================================

©Copyright 蕃薯耀 2021-12-22

​​​​​蕃薯耀的博客_CSDN博客



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3