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>
? 版權(quán)聲明
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載。
THE END