SpringBoot 配置文件中配置的中文,程序读取出来是乱码的解决

时间:2021-05-20

配置文件中是正常显示的中文,但是spring读取到的确是乱码。

我总共有两种解决办法,

第一种方法:

先复制或者备份一下你的配置文件的所有字符,打开设置将transparent native-to-ascii conversion选中,然后返回将之前的配置文件重新粘贴一遍(一定要将中文重新打一遍)如图:

Transparent native-to-ascii conversion的意思是:自动转换ASCII编码。

他的工作原理是:在文件中输入文字时他会自动的转换为Unicode编码,然后在idea中发开文件时他会自动转回文字来显示。

这样做是为了防止文件乱码。。。

OK,大概意思就是这样,这个文件你虽然看起来没问题,但是你只要选中了它,他么他现在就是一个ASCII文件存储在你本地,但是git上的文件可不是这个格式,你可以尝试用notepad++打开这个本地的.properties文件,你会发现他没有中文,,

这样可能会导致一个问题,git 提交后中文字符会乱码

第二种方法:

这个方法呢很简单就是直接在配置文件中将中文设置为Unicode编码,例如

spring.application-id=\u8863\u9f99\u5ddd

去网页找一个中文转成Unicode码的网站,直接进行转换

补充知识 :springboot 项目执行出现中文乱码(从本地运行到打war包)

前言:中文乱码问题

一,本地运行

就是直接使用springboot内嵌的tomcat运行出现中文乱码的问题

(1)参考如下pom.xml的配置文件加入jvm启动参数。

<jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>

(2)具体加的位置如下

<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!-- spring-boot:run 中文乱码解决 --> <configuration> <fork>true</fork> <!--增加jvm参数--> <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments> </configuration> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.5.RELEASE</version> </dependency> </dependencies></plugin>

二,打成war包乱码解决

问题描述

今天在使用 maven 打包spring boot 项目上线时,遇到一个坑,项目本地启动中文是没有乱码的 ,但是当我把打包好的jar ,扔向服务器时运行时,中文全部乱码,开始还以为是liuxn 本身一些配置我没有配置好,后来经过测试,打包的jar 文件本身中文就已经乱码,下面为本人调试修改后可以正常打包可执行jar并中文不乱码的pom.xml配置文件。

<!-- spring boot 项目打包成 可执行 jar 包 必须添加 , 打包方式 找到 当前项目目录 cmd 执行 mvn clean package --> <build> <plugins> <!-- 打包成可执行jar或者war,防止中文乱码,必须要下面这一个插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>utf-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- 这里为项目启动类--> <mainClass>com.zhenqinl.StartupApplication</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>

三,结尾给大家一个神坑Tomcat报错

严重: Unable to process Jar entry [META-INF/versions/9/module-info.class] from Jar [jar:file:/E:/eclipse-workspace/.metadata/.plugins
/org.eclipse.wst.server.core/tmp1/wtpwebapps/GymSystem/WEB-INF/lib/log4j-api-2.11.1.jar!/] for
annotationsorg.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 19at
org.apache.tomcat.util.bcel.classfile.Constant.readConstant(Constant.java:136)at org.apache.tomcat.util.bcel.classfile.ConstantPool.<init>
(ConstantPool.java:59)at
org.apache.tomcat.util.bcel.classfile.ClassParser.readConstantPool(ClassParser.java:208)at
org.apache.tomcat.util.bcel.classfile.ClassParser.parse(ClassParser.java:118)at
org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2055)at
org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:1931)at
org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfi
g.java:1897)at org.apache.catalina.startup.ContextConfig.pro

本人是直接下载一个Tomcat解决问题的,出现这个问题就是Tomcat的问题。

以上这篇SpringBoot 配置文件中配置的中文,程序读取出来是乱码的解决就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章