将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>
验证步骤
- 清理本地Maven仓库中旧的Jetty、Servlet API相关依赖
- 重新启动应用,确认启动成功
内容的提问来源于stack exchange,提问作者KOTA SAI KUMAR
相关产品推荐
相关产品推荐

