Spring Boot啟動失?。簀ava.lang.NoClassDefFoundError: jakarta/servlet/http/HttpSessionContext該如何解決?

Spring Boot啟動失?。簀ava.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依賴。若缺少,需添加,例如(版本號需根據(jù)實際情況調(diào)整):

立即學習Java免費學習筆記(深入)”;

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

provided表示該依賴在運行時由容器提供,無需包含在最終應用包中。如果應用服務器未提供此依賴,則需將scope改為compile。添加依賴后,重新構(gòu)建并運行項目。如果問題依然存在,檢查是否存在依賴沖突或版本不兼容問題。

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點贊5 分享