springboot3配置jetty容器
前言:springboot3使用jetty容器,在构建时报错
2023-05-25T11:18:54.345+08:00 WARN 48956 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server
Caused by: java.lang.NoClassDefFoundError: jakarta/servlet/http/HttpSessionContext
后来查找原因发现是由于springboot3使用的是jakarta.servlet-api的版本为6,而项目使用的jetty服务器在6里边缺少对应的api。
解决方案
控制springboot3中jakarta.servlet-api的版本:
gradle[build.gradle.kts]
ext["jakarta-servlet.version"] = "5.0.0"
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation("org.springframework.boot:spring-boot-starter-jetty")
modules {
module("org.springframework.boot:spring-boot-starter-tomcat") {
replacedBy("org.springframework.boot:spring-boot-starter-jetty", "Use Jetty instead of Tomcat")
}
}
}
maven[pom.xml]
<!--1使用dependencyManagement控制版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--指定版本-->
<properties>
<jakarta-servlet.version>5.0.0</jakarta-servlet.version>
</properties>
<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>
</dependencies>