如何在Spring boot中设置UTF-8字符编码?

时间:2023-01-06 11:06:20

I use spring-boot in my project, and I run this jar file which is built by spring-boot as a service on Centos. When I run this service:

我在我的项目中使用spring-boot,并运行这个jar文件,它是通过spring-boot作为Centos的服务构建的。当我运行这个服务:

service myApp start

服务myApp开始

I always get the below error messages:

我总是收到下面的错误信息:

2016-08-26 09:11:02.002 ERROR 31900 --- [           main] o.s.b.c.FileEncodingApplicationListener  : System property 'file.encoding' is currently 'ANSI_X3.4-1968'. It should be 'UTF-8' (as defined in 'spring.mandatoryFileEncoding').
2016-08-26 09:11:02.018 ERROR 31900 --- [           main] o.s.b.c.FileEncodingApplicationListener  : Environment variable LANG is 'null'. You could use a locale setting that matches encoding='UTF-8'.
2016-08-26 09:11:02.018 ERROR 31900 --- [           main] o.s.b.c.FileEncodingApplicationListener  : Environment variable LC_ALL is 'null'. You could use a locale setting that matches encoding='UTF-8'.
2016-08-26 09:11:02.031 ERROR 31900 --- [           main] o.s.boot.SpringApplication               : Application startup failed
java.lang.IllegalStateException: The Java Virtual Machine has not been configured to use the desired default character encoding (UTF-8).
    at org.springframework.boot.context.FileEncodingApplicationListener.onApplicationEvent(FileEncodingApplicationListener.java:74) ~[spring-boot-1.3.7.RELEASE.jar!/:1.3.7.RELEASE]

If I run this jar file directly, then this application runs properly.

如果我直接运行这个jar文件,那么这个应用程序就会正常运行。

java -jar target/myApp-1.0.jar

目标/ myApp-1.0.jar java jar

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.7.RELEASE)

......

2016-08-26 09:54:34.954 DEBUG 32035 --- [           main] o.s.w.s.resource.ResourceUrlProvider     : Found resource handler mapping: URL pattern="/**", locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@1817d444]
2016-08-26 09:54:35.051  INFO 32035 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8000 (http)
2016-08-26 09:54:35.053 DEBUG 32035 --- [           main] o.s.w.c.s.StandardServletEnvironment     : Adding [server.ports] PropertySource with highest search precedence
2016-08-26 09:54:35.061  INFO 32035 --- [           main] co.nz.myApplication           : Started myApplication in 12.339 seconds (JVM running for 13.183)

This is pom.xml:

这是pom.xml:

<project>
....

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.7.RELEASE</version>
    <relativePath/>
 </parent>

 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
 </properties>
 ....

 <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
                <jvmArguments>-Dfile.encoding=UTF8 -Dspring.profiles.active="production"</jvmArguments>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>springloaded</artifactId>
                    <version>1.2.5.RELEASE</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
 </build>
</project>

application.properties:

application.properties:

....
spring.mandatory-file-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
....

locale setting on Centos:

在Centos的场所设置:

$ locale

LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8

I check all files setting as possible as I do, however I still have no idea how to solve this problem. I would appreciate if you can give me any suggestion.

我尽可能的检查所有文件的设置,但是我还是不知道如何解决这个问题。如果你能给我任何建议,我将不胜感激。

4 个解决方案

#1


7  

SOLUTION

Add to config file (for application /var/app/app.jar it will be /var/app/app.conf) below line:

添加到配置文件中(用于应用程序/var/ app/app/app。jar它将是/var/app/app.conf)下面的行:

export LANG='en_US.UTF-8'

CAUSE

The problem is in linux systemd (service). I tested it with script /etc/init.d/test:

问题在于linux系统(服务)。我用script /etc/init.d/test测试了它:

#/bin/bash
locale

result of command $ /etc/init.d/test:

命令的结果$ /etc/init.d/test:

LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

and result of command $ service test:

和命令$服务测试结果:

LANG=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=

In spring boot jar file is build-in systemd script and before running spring boot is executed among others:

在spring boot jar文件中内置systemd脚本,在运行spring boot之前执行以下操作:

# Source any config file
configfile="$(basename "${jarfile%.*}.conf")"

# Initialize CONF_FOLDER location defaulting to jarfolder
[[ -z "$CONF_FOLDER" ]] && CONF_FOLDER="${jarfolder}"

# shellcheck source=/dev/null
[[ -r "${CONF_FOLDER}/${configfile}" ]] && source "${CONF_FOLDER}/${configfile}"

#2


1  

Finally I found a way so that I can launch Spring-boot project properly.

最后我找到了一种方法,使我能够正确地启动Spring-boot项目。

The solution is use sudo /etc/init.d/myApp start instead of service myApp start

解决方法是使用sudo /etc/ init。d/myApp start而不是myservice app start

#3


0  

The first line tells you that you should set system property 'file.encoding'. I think that's the problem. Please see: Setting the default Java character encoding?

第一行告诉您应该设置系统属性“file.encoding”。我认为这就是问题所在。请参见:设置默认的Java字符编码?

#4


0  

Based on my answer here: https://*.com/a/48952844/986160 you need to run:

根据我的回答:https://*.com/a/48952844/986160你需要运行:

mvn spring-boot:run -Drun.jvmArguments="-Dfile.encoding=UTF-8"

#1


7  

SOLUTION

Add to config file (for application /var/app/app.jar it will be /var/app/app.conf) below line:

添加到配置文件中(用于应用程序/var/ app/app/app。jar它将是/var/app/app.conf)下面的行:

export LANG='en_US.UTF-8'

CAUSE

The problem is in linux systemd (service). I tested it with script /etc/init.d/test:

问题在于linux系统(服务)。我用script /etc/init.d/test测试了它:

#/bin/bash
locale

result of command $ /etc/init.d/test:

命令的结果$ /etc/init.d/test:

LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

and result of command $ service test:

和命令$服务测试结果:

LANG=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=

In spring boot jar file is build-in systemd script and before running spring boot is executed among others:

在spring boot jar文件中内置systemd脚本,在运行spring boot之前执行以下操作:

# Source any config file
configfile="$(basename "${jarfile%.*}.conf")"

# Initialize CONF_FOLDER location defaulting to jarfolder
[[ -z "$CONF_FOLDER" ]] && CONF_FOLDER="${jarfolder}"

# shellcheck source=/dev/null
[[ -r "${CONF_FOLDER}/${configfile}" ]] && source "${CONF_FOLDER}/${configfile}"

#2


1  

Finally I found a way so that I can launch Spring-boot project properly.

最后我找到了一种方法,使我能够正确地启动Spring-boot项目。

The solution is use sudo /etc/init.d/myApp start instead of service myApp start

解决方法是使用sudo /etc/ init。d/myApp start而不是myservice app start

#3


0  

The first line tells you that you should set system property 'file.encoding'. I think that's the problem. Please see: Setting the default Java character encoding?

第一行告诉您应该设置系统属性“file.encoding”。我认为这就是问题所在。请参见:设置默认的Java字符编码?

#4


0  

Based on my answer here: https://*.com/a/48952844/986160 you need to run:

根据我的回答:https://*.com/a/48952844/986160你需要运行:

mvn spring-boot:run -Drun.jvmArguments="-Dfile.encoding=UTF-8"