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

Spring Boot项目启动报错:缺失javax.servlet.ServletContext Bean问题排查

问题分析与解决

核心原因:版本不兼容

你使用的Spring Boot 3.2.2与Springfox 2.9.2存在严重版本冲突:

  • Spring Boot 3.x基于Jakarta EE 9+,所有javax.*包已迁移至jakarta.*命名空间(例如javax.servlet.ServletContext变为jakarta.servlet.ServletContext)
  • Springfox 2.9.2基于Java EE开发,依赖旧的javax.*包,完全不兼容Spring Boot 3.x的Jakarta环境,这就是找不到javax.servlet.ServletContext Bean的根本原因。

另外你手动添加的spring-plugin-core 2.0.0.RELEASE也与Spring Boot 3.x不兼容,会引发额外依赖冲突。

解决方案

提供两种可行方案,优先推荐第二种:

方案一:降级Spring Boot到2.x版本

若坚持使用Springfox,需将Spring Boot版本降至2.7.x(Springfox 2.9.2支持的最高Spring Boot版本):
修改pom.xml中的父依赖版本:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.18</version>
    <relativePath/>
</parent>

同时可移除spring-plugin-core依赖,Spring Boot 2.x会自动管理兼容的版本。

方案二:替换Springfox为Springdoc(推荐)

Springfox已停止维护,Springdoc是Spring官方推荐的Swagger/OpenAPI替代方案,完美支持Spring Boot 3.x:

  1. 移除所有Springfox相关依赖及spring-plugin-core:
<!-- 删除以下依赖 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>org.springframework.plugin</groupId>
    <artifactId>spring-plugin-core</artifactId>
    <version>2.0.0.RELEASE</version>
</dependency>
  1. 添加Springdoc依赖:
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.3.0</version>
</dependency>
  1. 修改启动类,移除@EnableSwagger2注解,Springdoc会自动完成配置:
package com.leonard.joblisting;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;

@SpringBootApplication
public class JoblistingApplication {

    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title("Job Listing API")
                        .version("1.0")
                        .description("API文档:职位列表应用"));
    }

    public static void main(String[] args) {
        SpringApplication.run(JoblistingApplication.class, args);
    }
}
  1. 启动项目后,访问http://localhost:8080/swagger-ui/index.html即可查看API文档。

内容的提问来源于stack exchange,提问作者Thiện Hữu Huỳnh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 07:35:19