Spring Boot命令行运行程序正在使用Windows默认字符编码

发布于 2021-02-02 11:37:00

我在Windows上运行Spring
Boot应用程序,并使用Windows-1252编码。但是,独立的Java程序正在使用UTF-8编码。如何强制弹簧靴使用UTF-8。下面的代码输出????。我使用jar
-jar target \ spring-boot-example.jar使用以下命令

我验证了在Power Shell程序中默认字符集是Windows-1252(System.Text.Encoding):: Default

public class SpringBootConsoleApplication implements CommandLineRunner {
public static void main(String[] args) throws Exception {
    SpringApplication.run(SpringBootConsoleApplication.class, args);
}

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Default Charset=" +   Charset.defaultCharset());
        System.out.println("test::" +"الرياض");        
    }
}

我在application.properties中尝试了以下选项,但未成功:

# Charset of HTTP requests and responses. Added to the "Content-Type"    header if not set explicitly.
 spring.http.encoding.charset=UTF-8
 # Enable http encoding support.
 spring.http.encoding.enabled=true
 # Force the encoding to the configured charset on HTTP requests and   responses.
 spring.http.encoding.force=true
关注者
0
被浏览
86
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    @AbjitSarkar @JC Carrillo这个问题似乎与Windows环境有关。我在UbuntuVM上部署了相同的代码,一切似乎都很好。

    谢谢您的倾听,并给了我探索的秘诀!



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看