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

Spring Boot项目启动报错:方法不存在,求解决依赖兼容问题

依赖版本兼容问题排查与修复

问题核心原因

Spring Boot 3.0.0基于Jakarta EE 9规范,API包前缀从javax全面切换为jakarta,但你项目中存在多处版本不兼容的配置:

  1. 手动指定了tomcat-embed-core:9.0.73,而Tomcat 9.x仅支持旧的javax命名空间,Spring Boot 3.0.0调用的Jakarta版本方法addServletContainerInitializer在Tomcat 9中不存在,导致启动报错。
  2. spring-boot-starter-security:2.7.5是Spring Boot 2.x版本的依赖,与Spring Boot 3.x的Jakarta规范冲突,会引入大量不兼容的javax类。
  3. springfox-boot-starter:3.0.0不支持Spring Boot 3.x,仅兼容到Spring Boot 2.6.x。

修复步骤

1. 移除手动指定的Tomcat依赖

删除pom中手动添加的tomcat-embed-core依赖,让Spring Boot父项目自动管理兼容的Tomcat 10.x版本:

<!-- 移除该依赖 -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-core</artifactId>
    <version>9.0.73</version>
</dependency>

2. 统一Spring Security版本

将spring-boot-starter-security改为父项目管理的版本,同时移除单独的spring-security-crypto(spring-boot-starter-security已包含该依赖):

<!-- 替换原有security依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
    <!-- 移除手动指定的version,由父项目自动匹配3.0.0兼容版本 -->
</dependency>
<!-- 删除单独的spring-security-crypto依赖 -->

3. 替换Swagger依赖(Springfox → SpringDoc)

Springfox 3.0.0不支持Spring Boot 3.x,替换为SpringDoc OpenAPI:

<!-- 移除原有springfox依赖 -->
<!-- <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency> -->

<!-- 添加SpringDoc依赖 -->
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.0.2</version>
</dependency>

4. 移除不必要的手动版本指定

让Spring Boot父项目管理jakarta.persistence-api和hibernate-core的版本,避免冲突:

<!-- 修改后的依赖 -->
<dependency>
    <groupId>jakarta.persistence</groupId>
    <artifactId>jakarta.persistence-api</artifactId>
    <!-- 移除手动version -->
</dependency>
<dependency>
    <groupId>org.hibernate.orm</groupId>
    <artifactId>hibernate-core</artifactId>
    <!-- 移除手动version -->
</dependency>

5. 清理并重新构建

执行以下命令清理本地冲突依赖,重新构建项目:

mvn clean install

修复后核心依赖片段

<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.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-api</artifactId>
        <version>0.11.1</version>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-impl</artifactId>
        <version>0.11.1</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-jackson</artifactId>
        <version>0.11.1</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </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>
    <dependency>
        <groupId>jakarta.servlet</groupId>
        <artifactId>jakarta.servlet-api</artifactId>
    </dependency>
    <dependency>
        <groupId>jakarta.persistence</groupId>
        <artifactId>jakarta.persistence-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate.orm</groupId>
        <artifactId>hibernate-core</artifactId>
    </dependency>
    <!-- SpringDoc替代Springfox -->
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
        <version>2.0.2</version>
    </dependency>
</dependencies>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 02:14:56