utf-8编码

svujldwt  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(439)

自从我更新了windows10并安装了新的openjdk11.0.4之后,我的spring集成测试就不再在maven上下文中运行了。在eclipse中启动它很好,并且仍然可以工作,但是使用mvn clean install运行它却无法工作,错误如下:

18:01:01.340 ERROR [main]:[org.springframework.boot.context.FileEncodingApplicationListener] System property 'file.encoding' is currently 'Cp1252'. It should be 'UTF-8' (as defined in 'spring.mandatoryFileEncoding').
18:01:01.341 ERROR [main]:[org.springframework.boot.context.FileEncodingApplicationListener] Environment variable LANG is 'null'. You could use a locale setting that matches encoding='UTF-8'.
18:01:01.341 ERROR [main]:[org.springframework.boot.context.FileEncodingApplicationListener] Environment variable LC_ALL is 'null'. You could use a locale setting that matches encoding='UTF-8'.
18:01:01.344 ERROR [main]:[org.springframework.boot.SpringApplication] Application run 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:76)
    at org.springframework.boot.context.FileEncodingApplicationListener.onApplicationEvent(FileEncodingApplicationListener.java:47)

我已经设置了utf-8编码的地方,我可以设置。
在application-test.properties中

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

在maven属性和插件配置中:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

  <profiles>
    <profile>
      <id>Java 11</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <java.version>11</java.version>
      </properties>
      <build>
        <finalName>${project.artifactId}-${build-version}</finalName>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <release>${java.version}</release>
              <encoding>UTF-8</encoding>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
              <encoding>UTF-8</encoding>
            </configuration>
          </plugin>
        </plugins>
      </build>
      <dependencies>
        <dependency>
          <groupId>org.glassfish.jaxb</groupId>
          <artifactId>jaxb-runtime</artifactId>
          <version>2.4.0-b180830.0438</version><!--$NO-MVN-MAN-VER$ --> <!-- TODO move to stable version when available -->
        </dependency>
      </dependencies>
    </profile>

在集成测试属性中:

@RunWith(SpringRunner.class)
    @SpringBootTest(properties = "file.encoding=UTF-8", classes = SEBServer.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)

在将maven作为vm参数启动时
mvn clean安装-dfile.encoding=utf-8
有人能告诉我在哪里可以设置编码吗?天空中似乎有尽可能多的可能的地方,但没有一个合适的地方。
谢谢!

hfsqlsce

hfsqlsce1#

似乎编码 Cp1252 默认情况下,在某些旧版windows组件中使用(请参阅:https://en.wikipedia.org/wiki/windows-1252#:~:text=windows%2d1252%20or%20cp%2d1252,character%20encoding%20in%20the%20world)
正如op提到的,如果参数 -Dfile.encoding=UTF-8 已添加到 mvn 调用时,似乎我们需要一种在 pom.xml .
这可以通过包括 systemPropertyVariables 孩子在教室里 configuration 单元测试插件的元素 maven-surefire-plugin . 类似的 systemPropertyVariables 集成测试插件存在子级 maven-failsafe-plugin .
参考文献:https://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html

https://maven.apache.org/surefire/maven-failsafe-plugin/examples/system-properties.html

相关问题