前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot加载本地配置中文乱码

springboot加载本地配置中文乱码

作者头像
一笠风雨任生平
发布2022-01-06 14:22:42
6120
发布2022-01-06 14:22:42
举报
文章被收录于专栏:服务化进程服务化进程

背景

在开发过程中,加载本地配置出现中文乱码,加载方式如下:

代码语言:javascript
复制
@Component("xxxProperties")
@PropertySource(value = "classpath:001.xxx-config")
@ConfigurationProperties(prefix = "xxx-config", ignoreUnknownFields = true)
@Data
@Slf4j
public class RecSceneProperties {

}

配置文件内容如下:

代码语言:javascript
复制
xxx-config.scenes.4.xxx_id = 4
xxx-config.scenes.4.xxx_desc = 商品详情

在调试时发现显示中文乱码

解决方案

方案一

idea中文件格式调整 打开Settings>Editor>File Encodings ,

将Properties Files (*.properties)下的Default encoding for properties files设置为UTF-8,将Transparent native-to-ascii conversion前的勾选上。

在这里插入图片描述
在这里插入图片描述

网上建议的都是这种,但是没解决我的问题

方案二:

增加全局配置:

代码语言:javascript
复制
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8

该方案能解决问题

方案三

设置加载配置encoding

代码语言:javascript
复制
@Component("xxxProperties")
@PropertySource(value = "classpath:001.xxx-config", encoding = "UTF-8")
@ConfigurationProperties(prefix = "xxx-config", ignoreUnknownFields = true)
@Data
@Slf4j
public class RecSceneProperties {

}

方案四

将中文转为ASCII格式

代码语言:javascript
复制
xxx-config.scenes.4.xxx_desc =  \u5546\u54c1\u8be6\u60c5

能解决问题

总结

一般建议使用方案三

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021/05/19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • 解决方案
    • 方案一
      • 方案二:
        • 方案三
          • 方案四
          • 总结
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档