Spring Cloud 配置中心中文乱码问题解决

本文介绍了Spring Cloud配置中心出现中文乱码的问题及其解决方法。问题源于Spring默认使用PropertiesPropertySourceLoader加载配置,该加载器底层ISO 8859-1编码导致中文乱码。解决步骤包括:自定义属性源加载器,创建spring.factories文件并配置,以及在application.properties中增加相应设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

**

Spring Cloud 配置中心中文乱码问题解决

**
参考文章:https://blog.youkuaiyun.com/fan521dan/article/details/105084260
pom配置:org.springframework.boot spring-boot-starter-parent 2.3.3.RELEASE

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>

乱码原因:spring 默认使用 org.springframework.boot.env.PropertiesPropertySourceLoader 来加载配置,底层是通过调用 Properties 的 load 方法,而load方法输入流的编码是 ISO 8859-1 ,所以会出现中文乱码问题
解决路劲:

  • 解决步骤:
    1.自定义属性源加载器,实现 PropertySourceLoader 接口
    2.在 resources 文件夹下,创建 META-INF 文件夹,再创建 spring.factories 文件
    3.application.properties中增加配置

一、自定义属性源加载器:

public class CustomPropertySourceLoader implements PropertySourceLoader {
   

	    private static final Logger logger = LoggerFactory
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值