解决application.properties 中文乱码问题

情况描述

昨天开始学习SpringBoot,对配置文件application.properties进行了一些编码,今天来的时候再次打开application.properties这个配置文件,发现昨天编写的中文内容出现了下面乱码的情况,于是开始寻找解决方法。

2f6198d06d91444be70d318d65ed1bb

解决办法

1、在application.peoperties中添加如下内容

配置中文编码格式

server.tomcat.uri-encoding=UTF-8
server.servlet.encoding.force=true
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true

2、file—>settings—>file encoding

image-20240805173002258

3、保存退出——>重新启动IDEA 再次查看application.properties

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot项目中,如果application.properties文件中的中文出现乱码,这可能是因为Spring Boot默认使用的编码方式是ISO_8859_1。这意味着无论我们将application.properties文件保存为何种编码方式,最终它都会以ISO_8859_1的编码方式加载。 当我们的配置文件中包含中文字符时,如果不使用正确的编码方式进行设置,就会导致中文字符在加载时出现乱码。为了解决这个问题,我们可以使用UTF-8编码方式来保存和加载application.properties文件。可以在项目的启动类上添加如下注解来指定编码方式: ```java @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @PostConstruct public void init() { // 设置Spring Boot的编码方式为UTF-8 Charset charset = Charset.forName("UTF-8"); Charset.setDefaultCharset(charset); } } ``` 通过这种方式,我们可以确保application.properties文件中的中文字符能够正确加载,避免出现乱码问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [如何处理从application.properties配置文件获取的汉字乱码问题](https://blog.csdn.net/u013232219/article/details/122270606)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [application.properties中文乱码解决方案](https://blog.csdn.net/qq_43753724/article/details/122351883)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值