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

将Spring Boot内置服务器从Tomcat切换为Jetty时启动失败

Spring Boot替换Tomcat为Jetty启动失败的问题排查与解决

核心错误

启动时抛出ApplicationContextException,根源是类加载失败:

java.lang.NoClassDefFoundError: jakarta/servlet/http/HttpSessionContext

问题原因

HttpSessionContext是Jakarta Servlet 5及更早版本中的API,在Spring Boot 3.x默认依赖的Jakarta Servlet 6中已被移除。Jetty 11.x依赖Jakarta Servlet 5,与Spring Boot 3.x的Servlet API版本不兼容,导致启动时找不到该类。

修复方案

推荐方案:依赖Spring Boot版本管理

Spring Boot的spring-boot-starter-jetty会自动适配与当前Spring Boot版本兼容的Jetty版本(Spring Boot 3.1.x对应Jetty 12.x,完美支持Jakarta Servlet 6)。保持现有依赖结构即可,无需手动指定版本:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

执行清理构建命令:

mvn clean install

备选方案:手动指定兼容Jetty版本

若需锁定Jetty版本,必须选择支持Jakarta Servlet 6的Jetty 12.x系列:

<properties>
    <jetty.version>12.0.1</jetty.version>
</properties>

验证步骤

  1. 清理本地Maven仓库中旧的Jetty、Servlet API相关依赖
  2. 重新启动应用,确认启动成功

内容的提问来源于stack exchange,提问作者KOTA SAI KUMAR

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 16:45:15