Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nibexo opened this issue Dec 30, 2022 · 10 comments
Closed
Labels
status: duplicate A duplicate of another issue

Comments

@nibexo
Copy link

nibexo commented Dec 30, 2022

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 30, 2022
@kdebski85
Copy link

kdebski85 commented Dec 31, 2022

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

@philwebb
Copy link
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.

@philwebb
Copy link
Member

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

@philwebb philwebb closed this as not planned Won't fix, can't repro, duplicate, stale Dec 31, 2022
@philwebb philwebb added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 31, 2022
@nibexo
Copy link
Author

nibexo commented Jan 2, 2023

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.

@bclozel
Copy link
Member

bclozel commented Jan 2, 2023

@nibexo See #33044 (comment)

@Tak1za
Copy link

Tak1za commented Jan 12, 2023

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.

@tasosz
Copy link

tasosz commented Mar 31, 2023

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',

@nibexo
Copy link
Author

nibexo commented Mar 31, 2023

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

@hannah23280
Copy link

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?

@du-it
Copy link

du-it commented Sep 7, 2023

#33044

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

9 participants