java中设置utf-8_java - 如何在Spring启动时设置UTF-8字符编码? - 堆栈内存溢出

我在我的项目中使用spring-boot,并运行这个由spring-boot构建的jar文件作为Centos上的服务。 当我运行此服务时:

服务myApp启动

我总是收到以下错误消息:

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]

如果我直接运行此jar文件,则此应用程序正常运行。

java -jar target / myApp-1.0.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)

这是pom.xml:

....

org.springframework.boot

spring-boot-starter-parent

1.3.7.RELEASE

UTF-8

1.8

....

org.springframework.boot

spring-boot-maven-plugin

true

-Dfile.encoding=UTF8 -Dspring.profiles.active="production"

org.springframework

springloaded

1.2.5.RELEASE

application.properties:

....

spring.mandatory-file-encoding=UTF-8

spring.http.encoding.charset=UTF-8

spring.http.encoding.enabled=true

....

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

我像我一样检查所有文件设置,但是我仍然不知道如何解决这个问题。 如果你能给我任何建议,我将不胜感激。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值