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

为API编写过滤器时遭遇TypeNotPresentException错误求助

问题排查与解决:Spring Boot启动失败(javax.servlet.HttpServletRequest缺失)

核心错误原因

你使用的Springfox 3.0.0完全不兼容Spring Boot 3.x版本。Spring Boot 3.x已经全面迁移到Jakarta EE规范(包名从javax.*改为jakarta.*),但Springfox 3.0.0仍依赖旧的javax.servlet API,导致启动时无法找到对应类,抛出ClassNotFoundException: javax.servlet.http.HttpServletRequest。

另外你的依赖里存在重复配置:同时引入了springfox-boot-starter和单独的springfox-swagger2、springfox-swagger-ui;且knife4j-spring-boot-starter:3.0.3对Spring Boot 3的支持也不完善,容易引发冲突。

解决步骤

  • 移除所有Springfox依赖
    删掉build.gradle中这三行冗余且不兼容的依赖:

    implementation("io.springfox:springfox-swagger2:3.0.0")
    implementation("io.springfox:springfox-swagger-ui:3.0.0")
    implementation("io.springfox:springfox-boot-starter:3.0.0")
    
  • 替换为兼容Spring Boot 3的API文档工具
    推荐使用Spring官方维护的SpringDoc OpenAPI,它完全适配Jakarta EE规范:

    implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0")
    

    若需要Knife4j的增强功能,改用其Spring Boot 3适配版本:

    implementation("com.github.xiaoymin:knife4j-openapi3-jakarta-spring-boot-starter:4.4.0")
    
  • 移除冗余的Servlet依赖
    spring-boot-starter-web已经内置了Jakarta Servlet API,删掉单独引入的:

    implementation("jakarta.servlet:jakarta.servlet-api:5.0.0")
    
  • 更新JJWT依赖(可选但推荐)
    你当前使用的jjwt:0.9.1版本较老,建议升级到支持Jakarta的新版本:

    implementation("io.jsonwebtoken:jjwt-api:0.12.5")
    runtimeOnly("io.jsonwebtoken:jjwt-impl:0.12.5")
    runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.12.5")
    

最终依赖配置示例

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    // 替换为适配Spring Boot3的Knife4j或SpringDoc
    implementation("com.github.xiaoymin:knife4j-openapi3-jakarta-spring-boot-starter:4.4.0")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.postgresql:postgresql")
    implementation("com.google.code.gson:gson:2.8.8")
    // 升级后的JJWT依赖
    implementation("io.jsonwebtoken:jjwt-api:0.12.5")
    runtimeOnly("io.jsonwebtoken:jjwt-impl:0.12.5")
    runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.12.5")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

验证

重新构建并启动项目,javax.servlet相关的类找不到错误会消失。API文档可通过以下地址访问:

  • SpringDoc:http://localhost:8080/swagger-ui.html
  • Knife4j:http://localhost:8080/doc.html

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 07:37:05