Spring Boot - 字体很棒的OTS解析错误:无法转换

cod*_*ode 14 spring-mvc font-awesome spring-boot

在Spring Boot/Spring MVC应用程序中,字体无法正常工作的问题.

问题是所有字体文件都显示各种错误,如下所示

Failed to decode downloaded font: http://localhost:8080/fonts/fontawesome-webfont.woff2?v=4.4.0
OTS parsing error: Failed to convert WOFF 2.0 font to SFNT

Failed to decode downloaded font: http://localhost:8080/fonts/fontawesome-webfont.woff?v=4.4.0
OTS parsing error: incorrect file size in WOFF header

Failed to decode downloaded font: http://localhost:8080/fonts/fontawesome-webfont.ttf?v=4.4.0
OTS parsing error: incorrect entrySelector for table directory
Run Code Online (Sandbox Code Playgroud)

cod*_*ode 28

问题是Maven正在过滤字体文件并破坏它们.

    <resource>
        <directory>${project.basedir}/src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
Run Code Online (Sandbox Code Playgroud)

修复是对以下内容进行以下更改 pom.xml

    <resource>
        <directory>${project.basedir}/src/main/resources</directory>
        <filtering>true</filtering>
        <excludes>
            <exclude>static/fonts/**</exclude>
        </excludes>
    </resource>

    <resource>
        <directory>${project.basedir}/src/main/resources</directory>
        <filtering>false</filtering>
        <includes>
            <include>static/fonts/**</include>
        </includes>
    </resource>
Run Code Online (Sandbox Code Playgroud)

此更改允许在打包期间不过滤字体.


pos*_*e14 18

另一种方法是更新maven-resources-plugin的配置,如下所示:

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-resources-plugin</artifactId>
     <configuration>
          <nonFilteredFileExtensions>
               <nonFilteredFileExtension>ttf</nonFilteredFileExtension>
               <nonFilteredFileExtension>woff</nonFilteredFileExtension>
               <nonFilteredFileExtension>woff2</nonFilteredFileExtension>
          </nonFilteredFileExtensions>
     </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

  • 我个人觉得这个解决方案比公认的解决方案更清洁 (6认同)
  • 一个非常干净的解决方案。由于许多正当理由,应该被接受为答案。 (2认同)
  • 这是应该与 Spring Boot 入门项目的 POM 默认值一起使用的解决方案。 (2认同)