Skip to content

Spring Boot Test 3 NoClassDefFoundError: jakarta/servlet/ServletConnectionΒ #33661

Closed as not planned

Description

@nibexo

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

Activity

  1. kdebski85 commented on Dec 31, 2022

    @kdebski85
  2. philwebb commented on Dec 31, 2022

    @philwebb
    Member

    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.

  3. philwebb commented on Dec 31, 2022

    @philwebb
    Member

    I think we can consider this one a duplicate of #33044

  4. added and removed
    status: waiting-for-triageAn issue that has not yet been triaged. Contributions will not be accepted.
    on Dec 31, 2022
  5. nibexo commented on Jan 2, 2023

    @nibexo
    Author

    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

    @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.

  6. bclozel commented on Jan 2, 2023

    @bclozel
    Member
  7. Tak1za commented on Jan 12, 2023

    @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.

  8. tasosz commented on Mar 31, 2023

    @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',
    
  9. nibexo commented on Mar 31, 2023

    @nibexo
    Author

    @tasosz
    Did you try set only
    'org.eclipse.jetty:jetty-server:11.0.14' in implementation section?

  10. hannah23280 commented on Apr 12, 2023

    @hannah23280

    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.

    Hi, I do not understand. I thought , Jetty does not support servlet 6. How would adding latest version able to fix the problem?

  11. du-it commented on Sep 7, 2023

    @du-it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions