Spring Boot Web + Jetty (exclude Tomcat) + MockMvc 3.0.1 and jakarta-servlet.version 5.0.0
Error test on startup:
java.lang.NoClassDefFoundError: jakarta/servlet/ServletConnection
at org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.createServletRequest(MockHttpServletRequestBuilder.java:769)
at org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.buildRequest(MockHttpServletRequestBuilder.java:663)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:172)
at org.example.ExampleControllerTest.test(ExampleControllerTest.java:29)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
Caused by: java.lang.ClassNotFoundException: jakarta.servlet.ServletConnection
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 74 more
reproducer: https://github.com/nibexo/jetty-spring-boot-test-3-repro
Comment From: kdebski85
Spring Boot 3 requires Jakarta 6, not 5. Please see https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Migration-Guide#jakarta-ee
ServletConnection was added in Jakarta 6:
https://jakarta.ee/specifications/platform/10/apidocs/jakarta/servlet/servletconnection
Since: Servlet 6.0
Comment From: philwebb
Unfortunately at the time of release there was no Jetty version available that supported Servlet 6.0. We have an open issue to document how to downgrade dependencies. There's a lot of discussion in that issue with a few suggestions.
Comment From: philwebb
I think we can consider this one a duplicate of #33044
Comment From: nibexo
Spring Boot 3 requires Jakarta 6, not 5. Please see https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Migration-Guide#jakarta-ee
ServletConnectionwas added in Jakarta 6: https://jakarta.ee/specifications/platform/10/apidocs/jakarta/servlet/servletconnectionSince: Servlet 6.0
@kdebski85 Ok, but as this migration guide said:
Jetty does not yet support Servlet 6.0. To use Jetty with Spring Boot 3.0, you will have to downgrade the Servlet API to 5.0. You can use the jakarta-servlet.version property to do so.
I need to set Servlet API to 5.0 when I'm using Spring Boot 3 with Jetty, and it works, but then Spring tests throw exception.
Comment From: bclozel
@nibexo See https://github.com/spring-projects/spring-boot/issues/33044#issue-1438725435
Comment From: Tak1za
Found a temporary fix for the problem. Move back to Jakarta 6.0.0 and add the following dependency:
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
With the latest version of jetty-server.
With this the service starts up and the unit tests with MockMvc works as expected.
Comment From: tasosz
I've had the same issue, and the previously suggested options were not working for everything. Either the service would start or all the tests (inc mockmvc) would succeed. But not everything. :-(
In the end, this setup has worked for all:
ext {
set("jakarta-servlet.version", '5.0.0')
}
plus:
testImplementation
'org.eclipse.jetty:jetty-server:11.0.14',
'jakarta.servlet:jakarta.servlet-api:6.0.0',
Comment From: nibexo
@tasosz Did you try set only 'org.eclipse.jetty:jetty-server:11.0.14' in implementation section?
Comment From: hannah23280
Found a temporary fix for the problem. Move back to Jakarta 6.0.0 and add the following dependency:
```
``` org.eclipse.jetty jetty-server ${jetty.version} With the latest version of jetty-server.
With this the service starts up and the unit tests with MockMvc works as expected.
Hi, I do not understand. I thought , Jetty does not support servlet 6. How would adding latest version able to fix the problem?
Comment From: du-it