1. 程式人生 > 程式設計 >SpringBoot 配置檔案中配置的中文,程式讀取出來是亂碼的解決

SpringBoot 配置檔案中配置的中文,程式讀取出來是亂碼的解決

配置檔案中是正常顯示的中文,但是spring讀取到的確是亂碼。

我總共有兩種解決辦法,

第一種方法:

先複製或者備份一下你的配置檔案的所有字元,開啟設定將transparent native-to-ascii conversion選中,然後返回將之前的配置檔案重新貼上一遍(一定要將中文重新打一遍)如圖:

SpringBoot 配置檔案中配置的中文,程式讀取出來是亂碼的解決

Transparent native-to-ascii conversion的意思是:自動轉換ASCII編碼。

他的工作原理是:在檔案中輸入文字時他會自動的轉換為Unicode編碼,然後在idea中發開檔案時他會自動轉回文字來顯示。

這樣做是為了防止檔案亂碼。。。

OK,大概意思就是這樣,這個檔案你雖然看起來沒問題,但是你只要選中了它,他麼他現在就是一個ASCII檔案儲存在你本地,但是git上的檔案可不是這個格式,你可以嘗試用notepad++開啟這個本地的.properties檔案,你會發現他沒有中文,,

這樣可能會導致一個問題,git 提交後中文字元會亂碼 https://www.jb51.net/article/195655.htm

第二種方法:

這個方法呢很簡單就是直接在配置檔案中將中文設定為Unicode編碼,例如

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

去網頁找一箇中文轉成Unicode碼的網站,直接進行轉換

補充知識 :springboot 專案執行出現中文亂碼(從本地執行到打war包)

前言:中文亂碼問題

SpringBoot 配置檔案中配置的中文,程式讀取出來是亂碼的解決

一,本地執行

就是直接使用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 配置檔案中配置的中文,程式讀取出來是亂碼的解決就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。