Ramanujan R
Ramanujan R

Reputation: 1719

java.lang.ClassNotFoundException: jakarta.servlet.ServletConnection: Spring Boot and Jetty

Our project uses Spring Boot 3 and Jetty 11. As detailed here, there are some compatibility issues. So the jakarta.servlet-api:5.0.0 has been added explicitly.

<dependency>
   <artifactId>jakarta.servlet-api</artifactId>
   <groupId>jakarta.servlet</groupId>
   <version>5.0.0</version>
</dependency>
enter code here

We use Spring test containers and the dependencies are as shown below.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-testcontainers</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>1.19.8</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>mysql</artifactId>
        <scope>test</scope>
    </dependency>

With this setup, mvn clean install will fail with error java.lang.ClassNotFoundException: jakarta.servlet.ServletConnection. So I added a newer version of the servlet api in the test scope. Now there are two versions of th servlet-api, one in compile scope and the other in test scope.

    <dependency>
        <artifactId>jakarta.servlet-api</artifactId>
        <groupId>jakarta.servlet</groupId>
        <version>6.0.0</version>
        <scope>test</scope>
    </dependency>

Now the tests and clean install work. But, even if this is in the test scope, this error is happening when running the project - java.lang.ClassNotFoundException: jakarta.servlet.Filter. This is due to the compatibility issue mentioned in the above link.

But I added 6.0.0 version in the test scope. Still the error is happening when running the project. How to resolve this?

Note: Changed 5.0.0 to "provided" and "runtime" scopes also. No luck.

EDIT: As per this answer, it seems to be a feature change in maven 3 that it will attempt to obtain the nearest dependency, effectively ensuring that only one of the compile or test scoped dependency is used for both the compile and test phases. Is there any valid fix for the original problem?

Upvotes: 0

Views: 160

Answers (0)

Related Questions