在我的gradle基础项目中,我试图将spring依赖从2.7.6升级到3.0.1,
然而我面临问题NoClassDefFoundError: jakarta/servlet/http/HttpSessionContext
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:158)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:183)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:161)
... 9 common frames omitted
Caused by: java.lang.ClassNotFoundException: jakarta.servlet.http.HttpSessionContext
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 19 common frames omitted
据我所知,spring boot 3.0使用Servlet Api 6.0,这是Jetty不支持的,它需要将Servlet Api降级到5.0.0(https://github.com/spring - projects/spring boot/wiki/spring -引导- 3.0 -迁移-指南# jetty)
要解决这个问题,您需要添加到您的gradle.build
脚本ext['jakarta-servlet.version'] = '5.0.0'
我也遇到过同样的问题,以前建议的选项并不适用于所有情况。要么服务启动,要么所有测试(包括mockmvc)都成功。但不是所有的。设置5.0.0
或6.0.0
只能使一部分工作…: - (
最后,这个设置对所有人都有效:
ext {
set("jakarta-servlet.version", '5.0.0')
}
+:
testImplementation
'org.eclipse.jetty:jetty-server:11.0.14',
'jakarta.servlet:jakarta.servlet-api:6.0.0',