直接替换即可,无需修改业务代码,核心两步:排除Tomcat依赖并引入Undertow Starter;在pom.xml中为spring-boot-starter-web添加exclusions移除spring-boot-starter-tomcat,再单独添加spring-boot-starter-undertow依赖。

直接替换就行,不用改代码,核心就两步:排除 Tomcat 依赖、引入 Undertow Starter。
修改 Maven 依赖
在 pom.xml 中调整 spring-boot-starter-web 的依赖声明,移除内置 Tomcat,并添加 Undertow 支持:
- 用
<exclusions>去掉spring-boot-starter-tomcat - 单独加上
spring-boot-starter-undertow依赖
示例配置如下:
<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-undertow</artifactId>
</dependency>
Gradle 项目怎么处理
在 build.gradle 中,用 configurations 排除 Tomcat,再声明 Undertow:
立即学习“Java免费学习笔记(深入)”;
compile.exclude module: "spring-boot-starter-tomcat"implementation 'org.springframework.boot:spring-boot-starter-undertow'
注意:Spring Boot 2.3+ 推荐用 implementation 替代旧的 compile。
启动后验证是否生效
运行应用,观察控制台日志:
- 如果看到类似
UndertowWebServerFactory或Undertow started on port的输出,说明已成功切换 - 不再出现
TomcatWebServer相关日志 - 默认端口仍是
8080,除非你主动改了server.port
可选:自定义 Undertow 参数
在 application.yml 中可以调优关键参数:
-
server.undertow.io-threads:建议设为 CPU 核心数(如 4 或 8),别盲目调大 -
server.undertow.worker-threads:默认是 io-threads × 8,高负载时可适当提高 -
server.undertow.buffer-size和buffers-per-region:影响内存使用,一般保持默认即可 -
server.http2.enabled=true:启用 HTTP/2(需 JDK 9+ 和客户端支持)
不复杂但容易忽略


















