21

I community, I'm trying to run a small example with Spring boot 3 and Jetty server before upgrading the production code but I'm getting this error java.lang.ClassNotFoundException: jakarta.servlet.http.HttpSessionContext and the services does not start. This is my Gradle config.

plugins {
    id 'java'
    id 'idea'
    id 'org.springframework.boot' version '3.0.1'
    id 'io.spring.dependency-management' version '1.1.0'
}

idea {
    module {
        downloadJavadoc = false
        downloadSources = false
    }
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-security'

    implementation 'org.springframework.boot:spring-boot-starter-jetty'
    implementation('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
    }

    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

and the dependeincies.

dependencies

HttpSessionContext class no longer exists but somehow, the latest version Jetty still depends on it.

I'm expecting to make it run with Jetty without migrating to another server.

1
  • Checkout this post. It explains for both Maven and Gradle. youtu.be/1gEoiMVULt4 Commented Oct 3, 2023 at 10:56

9 Answers 9

15

Update 23.12.13

No workaround needed anymore as Jetty released version 12 with support for servlet 6.0.0. So just update to spring-boot 3.2.0 (spring-boot-starter-jetty:3.2.0) which includes jetty 12.

Origin

As Joakim Erdfelt already mentioned, Spring Boot 3 rely on Jakarta Servlet 6.0.0 (see https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Release-Notes) and the spring-boot-starter-jetty includes Jetty 11 which is build on Jakarta Servlet 5.0.0 (see https://java.libhunt.com/jetty-project-changelog/11.0.0). So this is an Issue in the starter itself.

To use jetty you have to downgrade the jakarta-servlet version (see https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Migration-Guide#jetty) setting

ext["jakarta-servlet.version"] = "5.0.0"
Sign up to request clarification or add additional context in comments.

6 Comments

thanks mathze, this is a big mistake from the spring team. Looks that they didn't test it :(
Wouldn't say it's a mistake. But maybe spring-team could had make this information better visible in their documentation ;)
For me, it's a mistake, because the starter dependencies are wrong. jetty starter must use Jakarta 5.0 and not the 6.0
I agree with @red. You can see in mvnrepository.com/artifact/org.springframework.boot/… that they declared spring-boot-starter-jetty:3.0.2 to depend on jakarta.servlet-api:6.0.0 and org.eclipse.jetty:jetty-*:11.0.13 and they are not compatible. On a related note, for those using Maven, simply declared a property in your om.xml like this one: <jakarta-servlet.version>5.0.0</jakarta-servlet.version> and that's it.
if you downgrade to jakarta-servlet 5.0.0 then spring-test 6 doesn't compile anymore :)
|
10

For Spring Boot 3 and Jetty, you need a couple of dependencies.

  • need the jakarta.servlet-api
  • need the jetty-server
  • need the spring-boot-starter-jetty

here is an example along with the versions

    <dependency>
        <groupId>jakarta.servlet</groupId>
        <artifactId>jakarta.servlet-api</artifactId>
        <version>6.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>11.0.14</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>

Comments

9

Just wanted to share my experience with Spring Boot 3 and Java 17. I recently encountered an issue with my pom.xml while working on a project. However, I was able to resolve it by making a few adjustments.

To fix the problem, I excluded Tomcat from the spring-boot-starter-web dependency and instead added spring-boot-starter-jetty to my project. However, I noticed that Maven was not able to fetch the necessary dependency automatically.

To overcome this, I added the jakarta-servlet.version property with a value of 5 in my pom.xml properties file. This ensured that Maven correctly resolved the required dependency.

If you're facing a similar issue, you can try out the following pom.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.5</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<!-- Add this property -->
<properties>
    <java.version>17</java.version>
    <jakarta-servlet.version>5.0.0</jakarta-servlet.version>
</properties>

<!-- Exclude Tomcat and add Jetty -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <groupId>org.springframework.boot</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
</project>

I hope this helps! Let me know if you have any questions or if there's anything else I can assist you with.

Happy coding! 👨‍💻

2 Comments

That was the only part missing from all the other searches results <jakarta-servlet.version>5.0.0</jakarta-servlet.version> at least for now
Exactly, this is the missing property "jakarta-servlet.version".
5

Add this line to your pom.xml properties:

<jakarta-servlet.version>5.0.0</jakarta-servlet.version>

Comments

3

Spring 3 is for Servlet 6 which is available in Jetty 12+

Comments

1

For Spring boot 3.x onward extra jetty-server dependancy is needed...

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
        </dependency>

Comments

0

Pinning the version like

ext {
    set("jakarta-servlet.version", '5.0.0')
}

did not worked for me, using Spring Boot 3.1.5!

What helps at my side, is to strictly set the version using an implementation constraint:

implementation("org.springframework.boot:spring-boot-starter-jetty") {
    constraints {
        implementation("jakarta.servlet:jakarta.servlet-api") {
            version {
                strictly("5.0.0")
                prefer("5.0.0")
            }
            because("Jetty 11.x requires jakarta.servlet-api 5.0.0 where version 6.0.0 is bundled with spring-boot-starter-jetty (Spring Boot 3.1.5)!")
        }
    }
}

Comments

0

In my case for Spring 3.5.2

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
    <dependency>
        <groupId>jakarta.platform</groupId>
        <artifactId>jakarta.jakartaee-api</artifactId>
        <version>11.0.0-M2</version>
    </dependency>

Comments

0

I was facing the same issue, Here is what I did to resolve this.

  • updated spring-boot-starter-jetty with 3.2.0

  • updated spring boot starter to 3.2.0 versions

  • added below dependency

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

hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.