You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何SpringBoot可执行JAR仅抛出白标错误?

Spring Boot JAR启动间歇性白标404问题排查

问题描述

开发的SpringBoot应用通过mvn clean package生成可执行JAR,在IntelliJ中启动运行正常,但CMD启动JAR时会间歇性出现白标404错误——有时能正常访问,刷新或切换HTML页面时错误再次出现,仅显示无来源信息的404提示。

应用基础信息:

  • Application类无父包,带有@SpringBootApplication注解
  • 技术栈:PostgreSQL数据库、Bootstrap前端框架
  • 已尝试的无效方案:添加@ComponentScan("...")、@SpringBootApplication(scanBasePackages = "...")、更换WLAN、关闭防火墙、统一类包路径、更换JDK、更换运行电脑

配置与日志信息

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>at.test</groupId>
    <artifactId>Test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Testname</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

运行日志

2022-08-10 13:09:05.217 DEBUG 18536 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : GET "/", parameters={}
2022-08-10 13:09:05.248 DEBUG 18536 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [ServletContext [/]]
2022-08-10 13:09:05.264 DEBUG 18536 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2022-08-10 13:09:05.264 DEBUG 18536 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
2022-08-10 13:09:05.279 DEBUG 18536 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
2022-08-10 13:09:05.279 DEBUG 18536 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2022-08-10 13:09:05.358 DEBUG 18536 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2022-08-10 13:09:05.374 DEBUG 18536 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 404

解决方案与排查方向

从日志可见,核心问题是请求根路径/时找不到对应资源,间歇性出现说明资源加载存在不稳定因素,可按以下步骤排查:

  1. 静态资源路径校验

    • 确认HTML等静态文件存放位置:Spring Boot默认读取src/main/resources/static、src/main/resources/public、src/main/resources/resources目录,若自定义路径需在application.properties/application.yml中配置spring.web.resources.static-locations
    • 解压打包后的JAR,检查静态资源是否被正确打包到BOOT-INF/classes/static(或对应自定义目录)下,避免Maven打包时遗漏
  2. 优化Maven打包配置

    • 添加显式资源打包配置,确保所有资源被正确复制:
      <build>
          <resources>
              <resource>
                  <directory>src/main/resources</directory>
                  <includes>
                      <include>**/*</include>
                  </includes>
              </resource>
          </resources>
          <!-- 保留原有plugins配置 -->
      </build>
      
    • 使用mvn clean package -DskipTests打包,跳过测试但不跳过资源复制步骤
  3. 视图解析器与模板引擎配置

    • 若使用模板页面(非纯静态HTML),需添加对应starter依赖(以Thymeleaf为例):
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
      
    • 纯静态HTML可添加Controller映射根路径:
      @Controller
      public class HomeController {
          @GetMapping("/")
          public String home() {
              return "index.html";
          }
      }
      
  4. JAR运行时资源访问方式

    • 避免使用文件系统绝对路径读取资源,改用Spring提供的ResourceLoader或ClassPathResource加载类路径资源:
      @Autowired
      private ResourceLoader resourceLoader;
      
      public Resource getStaticResource() {
          return resourceLoader.getResource("classpath:static/index.html");
      }
      
  5. 版本兼容性排查

    • 当前使用Spring Boot 2.5.6,可升级至同系列稳定版本(如2.5.15),排查是否存在版本特定的资源加载Bug

内容的提问来源于stack exchange,提问作者Celina

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 23:15:44