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

Spring Cloud Config Server启动报错:jakarta.servlet.HttpServletRequest缺失

问题排查与解决

核心原因

TypeNotPresentException: 无法找到jakarta.servlet.http.HttpServletRequest 本质是Servlet API依赖在运行时缺失。你将spring-boot-starter-tomcat的scope设为provided,意味着该依赖仅在编译阶段可用,本地启动应用时不会被加载,而Spring Cloud Config Server运行需要Servlet容器相关类支持。

另外需确认Spring Boot版本:Spring Boot 3.x系列依赖Jakarta EE规范(包路径jakarta.servlet.*),2.x系列依赖Java EE(javax.servlet.*),版本不匹配也会引发类找不到的问题。

具体解决步骤

1. 调整Tomcat依赖的Scope

本地开发启动时,将spring-boot-starter-tomcat的scope从provided改为默认的compile,确保运行时能加载该依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <!-- 移除scope="provided",或显式设置为compile -->
</dependency>

若生产环境需部署到外部Tomcat,再改回provided即可,避免依赖冲突。

2. 校验Spring Boot与Spring Cloud版本兼容性

确保两者版本匹配:

  • Spring Boot 3.x 对应 Spring Cloud 2022.0.x(代号Kilburn)及以上
  • Spring Boot 2.x 对应 Spring Cloud 2021.0.x(代号Jubilee)及更早版本

可通过Spring Cloud BOM统一管理版本,避免依赖冲突:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2022.0.4</version> <!-- 适配Spring Boot 3.0+ -->
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

3. 清理并重新构建项目

执行命令清理缓存、重新下载依赖:

mvn clean install -U

4. 验证主类配置

确认主类上的@EnableConfigServer注解正确,无额外Servlet配置冲突:

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

额外排查点

  • 若使用Jetty、Undertow等其他Servlet容器,需确保对应starter依赖已正确引入,且版本与Spring Boot匹配
  • 检查pom.xml是否存在重复的Servlet API依赖,避免版本冲突

内容的提问来源于stack exchange,提问作者P Yoga Prudhvi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 01:35:57