java.lang.ClassNotFoundException:带有 Spring Boot 3 和 Jetty 服务器的 jakarta.servlet.http.HttpSessionContext

red*_*red 17 spring jetty embedded-jetty spring-boot

在社区中,我尝试在升级生产代码之前使用 Spring boot 3 和 Jetty 服务器运行一个小示例,但我收到此错误java.lang.ClassNotFoundException: jakarta.servlet.http.HttpSessionContext并且服务无法启动。这是我的 Gradle 配置。

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()
}

Run Code Online (Sandbox Code Playgroud)

和依赖性。

依赖关系

HttpSessionContext 类不再存在,但不知何故,最新版本的 Jetty 仍然依赖于它。

我希望让它与 Jetty 一起运行,而无需迁移到另一台服务器。

mat*_*hze 13

2013年12月23日更新

由于 Jetty 发布了支持 servlet 6.0.0 的版本 12,因此不再需要解决方法。因此,只需更新到 spring-boot 3.2.0 (spring-boot-starter-jetty:3.2.0),其中包括 jetty 12。

起源

正如 Jaokim Erdfelt 已经提到的,Spring Boot 3 依赖于 Jakarta Servlet 6.0.0(请参阅https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Release-Notes)和 spring- boot-starter-jetty 包括基于 Jakarta Servlet 5.0.0 构建的 Jetty 11(请参阅https://java.libhunt.com/jetty-project-changelog/11.0.0)。所以这是启动器本身的问题。

要使用 jetty,您必须降级 jakarta-servlet 版本(请参阅https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Migration-Guide#jetty)设置

ext["jakarta-servlet.version"] = "5.0.0"
Run Code Online (Sandbox Code Playgroud)

  • 我同意@red 的观点。您可以在 https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jetty/3.0.2 中看到他们声明 spring-boot-starter-jetty:3.0.2 依赖于 jakarta。 servlet-api:6.0.0 和 org.eclipse.jetty:jetty-*:11.0.13 并且它们不兼容。与此相关的是,对于使用 Maven 的用户,只需在 om.xml 中声明一个属性,如下所示:`<jakarta-servlet.version>5.0.0</jakarta-servlet.version>` 就这样了。 (5认同)
  • 谢谢mathze,这是Spring团队的一个大错误。看来他们没有测试过:( (2认同)

Faz*_*oon 9

只是想分享我使用 Spring Boot 3 和 Java 17 的经验。最近我在处理一个项目时遇到了 pom.xml 的问题。不过,我通过一些调整就解决了这个问题。

\n

为了解决这个问题,我从spring-boot-starter-web依赖项中排除了 Tomcat,而是将spring-boot-starter-jetty添加到了我的项目中。但是,我注意到 Maven 无法自动获取必要的依赖项。

\n

为了克服这个问题,我添加了jakarta-servlet.version在 pom.xml 属性文件中添加了值为 5 的这确保了 Maven 正确解析了所需的依赖关系。

\n

如果您遇到类似的问题,可以尝试以下 pom.xml 配置:

\n
<?xml version="1.0" encoding="UTF-8"?>\n<project xmlns="http://maven.apache.org/POM/4.0.0" \nxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\nxsi:schemaLocation="http://maven.apache.org/POM/4.0.0 \nhttps://maven.apache.org/xsd/maven-4.0.0.xsd">\n<modelVersion>4.0.0</modelVersion>\n<parent>\n    <groupId>org.springframework.boot</groupId>\n    <artifactId>spring-boot-starter-parent</artifactId>\n    <version>3.0.5</version>\n    <relativePath/> <!-- lookup parent from repository -->\n</parent>\n<groupId>com.example</groupId>\n<artifactId>demo</artifactId>\n<version>0.0.1-SNAPSHOT</version>\n<name>demo</name>\n<description>Demo project for Spring Boot</description>\n\n<!-- Add this property -->\n<properties>\n    <java.version>17</java.version>\n    <jakarta-servlet.version>5.0.0</jakarta-servlet.version>\n</properties>\n\n<!-- Exclude Tomcat and add Jetty -->\n<dependencies>\n    <dependency>\n        <groupId>org.springframework.boot</groupId>\n        <artifactId>spring-boot-starter-web</artifactId>\n        <exclusions>\n            <exclusion>\n                <artifactId>spring-boot-starter-tomcat</artifactId>\n                <groupId>org.springframework.boot</groupId>\n            </exclusion>\n        </exclusions>\n    </dependency>\n\n    <dependency>\n        <groupId>org.springframework.boot</groupId>\n        <artifactId>spring-boot-starter-jetty</artifactId>\n    </dependency>\n\n    <dependency>\n        <groupId>org.springframework.boot</groupId>\n        <artifactId>spring-boot-starter-test</artifactId>\n        <scope>test</scope>\n    </dependency>\n</dependencies>\n\n<build>\n    <plugins>\n        <plugin>\n            <groupId>org.springframework.boot</groupId>\n            <artifactId>spring-boot-maven-plugin</artifactId>\n        </plugin>\n    </plugins>\n</build>\n</project>\n
Run Code Online (Sandbox Code Playgroud)\n

我希望这有帮助!如果您有任何疑问或者还有什么我可以帮助您的,请告诉我。

\n

快乐编码!\xe2\x80\x8d

\n


Mah*_*lam 8

对于 Spring Boot 3 和 Jetty,您需要一些依赖项。

  • 需要jakarta.servlet-api
  • 需要jetty-server
  • 需要spring-boot-starter-jetty

这是一个示例以及版本

    <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>
Run Code Online (Sandbox Code Playgroud)


Bug*_*low 5

将此行添加到您的 pom.xml 属性中:

<jakarta-servlet.version>5.0.0</jakarta-servlet.version>
Run Code Online (Sandbox Code Playgroud)