UTF-8 wrong character encoding with spring-boot-devtools

问题: IDE settings UTF-8, OS Windows 10, Russian language. Code for console output: System.out.println("тест") Spring Boot without "spring-boot-devtools" dependency in IDEA...

问题:

IDE settings UTF-8, OS Windows 10, Russian language. Code for console output:

System.out.println("тест")

Spring Boot without "spring-boot-devtools" dependency in IDEA is OK

Spring Boot with "spring-boot-devtools" in Windows console is OK

Spring Boot with "spring-boot-devtools" in IDEA problem: enter image description here


回答1:

The following configuration in pom.xml will fix the problem:

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <executable>true</executable>
          <jvmArguments>-Dfile.encoding=UTF8</jvmArguments>
        </configuration>
      </plugin>
    </plugins>
  </build>

Explanation:

On Windows console encoding is cp1251 with your locale configuration, while IntelliJ IDEA console encoding is UTF-8.

When running from IntelliJ IDEA, -Dfile.encoding=UTF8 option is added to the command line when IDE is starting Maven so that Maven JVM output encoding is the same as the IDE console encoding.

Without spring-boot-devtools this option is passed correctly and is honored by the JVM process running your code in the IDE console. It's the same JVM instance that is running Maven, everything works fine.

With spring-boot-devtools dependency Maven JVM forks another JVM to run your code and file encoding option is no longer passed to it. File encoding defaults to the system locale for the new JVM process and becomes cp1251 while IntelliJ IDEA console is still set to use UTF-8 encoding. Output is broken as shown on your screenshot.

spring-boot-maven-plugin jvmArguments configuration should be used to pass the encoding option to the forked JVM. It also forces Windows console to the same encoding, so the app will continue to work correctly in the command line.

  • 发表于 2019-03-12 23:13
  • 阅读 ( 308 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除