14

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.

red
  • 606
  • 10
  • 17

5 Answers5

10

As Jaokim 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"
mathze
  • 458
  • 1
  • 6
  • 11
  • 2
    thanks mathze, this is a big mistake from the spring team. Looks that they didn't test it :( – red Dec 29 '22 at 14:48
  • Wouldn't say it's a mistake. But maybe spring-team could had make this information better visible in their documentation ;) – mathze Jan 01 '23 at 11:21
  • 1
    For me, it's a mistake, because the starter dependencies are wrong. jetty starter must use Jakarta 5.0 and not the 6.0 – red Jan 02 '23 at 17:22
  • 4
    I agree with @red. You can see in https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jetty/3.0.2 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: `5.0.0` and that's it. – GreenEyed Feb 06 '23 at 11:49
  • if you downgrade to jakarta-servlet 5.0.0 then spring-test 6 doesn't compile anymore :) – kosta.dani Aug 07 '23 at 11:52
6

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>
Mahbub Ul Islam
  • 773
  • 8
  • 15
6

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! ‍

Fazal Haroon
  • 845
  • 2
  • 10
  • 20
3

Add this line to your pom.xml properties:

<jakarta-servlet.version>5.0.0</jakarta-servlet.version>
Moritz Ringler
  • 9,772
  • 9
  • 21
  • 34
BugsOverflow
  • 386
  • 3
  • 19
1

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

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136