首页 > 文章列表 > Spring Boot启动失败:java.lang.NoClassDefFoundError: jakarta/servlet/http/HttpSessionContext该如何解决?

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

387 2025-04-10

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.(ServletContextHandler.java:180)
at org.eclipse.jetty.webapp.WebAppContext.(WebAppContext.java:301)
at org.eclipse.jetty.webapp.WebAppContext.(WebAppContext.java:228)
at org.springframework.boot.web.embedded.jetty.JettyEmbeddedWebAppContext.(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依赖。若缺少,需添加,例如(版本号需根据实际情况调整):


    jakarta.servlet
    jakarta.servlet-api
    YOUR_VERSION_HERE
    provided

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

来源:1741487361