Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.ClassNotFoundException: jakarta.servlet.http.HttpSessionContext with Spring Boot 3 and Jetty server

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.

like image 313
red Avatar asked Sep 13 '25 12:09

red


2 Answers

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"
like image 162
mathze Avatar answered Sep 16 '25 06:09

mathze


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>
like image 24
Mahbub Ul Islam Avatar answered Sep 16 '25 08:09

Mahbub Ul Islam