Spring Boot启动失败:java.lang.NoClassDefFoundError: jakarta/servlet/http/HttpSessionContext该如何解决?

Spring Boot启动失败:java.lang.NoClassDefFoundError: jakarta/servlet/http/HttpSessionContext该如何解决?

spring Boot项目启动失败:深入解析Java.lang.NoClassDefFoundError: jakarta/servlet/http/HttpSessionContext

spring boot应用启动时,各种错误层出不穷。本文分析一个常见的启动失败案例,错误信息如下:

Caused by: java.lang.NoClassDefFoundError: jakarta/servlet/http/HttpSessionContext at org.eclipse.jetty.servlet.ServletContextHandler.newSessionHandler(ServletContextHandler.java:339) at org.eclipse.jetty.servlet.ServletContextHandler.getSessionHandler(ServletContextHandler.java:432) at org.eclipse.jetty.servlet.ServletContextHandler.relinkHandlers(ServletContextHandler.java:257) at org.eclipse.jetty.servlet.ServletContextHandler.<init>(ServletContextHandler.java:180) at org.eclipse.jetty.webapp.WebAppContext.<init>(WebAppContext.java:301) at org.eclipse.jetty.webapp.WebAppContext.<init>(WebAppContext.java:228) at org.springframework.boot.web.embedded.jetty.JettyEmbeddedWebAppContext.<init>(JettyEmbeddedWebAppContext.java:28) at org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory.getWebServer(JettyServletWebServerFactory.java:159) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:183) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:161) ... 13 common frames omitted

该错误提示找不到jakarta/servlet/http/HttpSessionContext类,通常是因为缺少Servlet API依赖。HttpSessionContext属于Jakarta Servlet API,而较旧项目可能使用javax.servlet.http.HttpSessionContext。Spring Boot 2.x及以上版本已迁移至Jakarta Servlet API,因此需确保项目包含正确的Jakarta Servlet API依赖。

解决方法:检查pom.xml(maven项目)或build.gradle(Gradle项目),确认是否包含正确的Jakarta Servlet依赖。若缺少,需添加,例如(版本号需根据实际情况调整):

立即学习Java免费学习笔记(深入)”;

<dependency>     <groupId>jakarta.servlet</groupId>     <artifactId>jakarta.servlet-api</artifactId>     <version>YOUR_VERSION_HERE</version>     <scope>provided</scope> </dependency>

provided表示该依赖在运行时由容器提供,无需包含在最终应用包中。如果应用服务器未提供此依赖,则需将scope改为compile。添加依赖后,重新构建并运行项目。如果问题依然存在,检查是否存在依赖冲突或版本不兼容问题。

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享