Spring Boot 4.0官宣: 弃用 Undertow:Tomcat笑麻了

Spring Boot 4.0.0 M2 里程碑版本发布后,社区反响热烈,其中最引人关注的变动之一就是 正式移除对 Undertow 的内嵌支持。本文将深入解析这一决策背后的技术原因、对现有项目的影响,以及开发者应如何平稳过渡。


一、Undertow 被弃用:不是"抛弃",而是"规范对齐"

在 Spring Boot 3.x 及更早版本中,开发者可以选择三种内嵌 Web 容器:

  • Tomcat(默认)
  • Jetty
  • Undertow

其中,Undertow 因其 低内存占用、高并发吞吐能力、天然支持持久连接 等优势,被不少高并发场景下的企业项目采用。

然而,在 Spring Boot 4.0 中,官方彻底移除了对 Undertow 的支持。这不是 Spring 团队的主观决策,而是技术生态演进的必然结果:

Undertow 尚未适配 Servlet 6.1 规范 ,而 Spring Boot 4.0 基于 Spring Framework 7 ,强制依赖 Servlet 6.1

由于 Red Hat(Undertow 的维护方)对该项目的投入有限,导致 Undertow 无法及时跟进新规范。Spring Boot 无法在缺乏底层支持的情况下继续维护兼容层,因此只能做出移除决定。

关键结论:只要 Undertow 未来支持 Servlet 6.1,Spring Boot 仍有可能恢复对其支持。


二、对现有项目的影响

如果你的项目当前使用了如下配置:

yaml 复制代码
server:
  servlet:
    context-path: /api
  undertow:
    io-threads: 4
    worker-threads: 20
    buffer-size: 1024

或依赖:

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

那么在升级到 Spring Boot 4.0 后,项目将无法启动,并抛出类似错误:

vbnet 复制代码
Caused by: java.lang.IllegalStateException: 
  Unable to find Undertow-based WebServer implementation.

风险点总结:

风险项 说明
启动失败 缺少 Undertow 实现类
配置失效 server.undertow.* 配置被忽略
性能回退 切换容器后需重新压测调优

三、迁移方案:如何平滑过渡?

方案 1:切换回 Tomcat(推荐)

Tomcat 是 Spring Boot 默认容器,生态成熟、文档丰富,且在 Spring Boot 4 中已全面适配 Servlet 6.1。

步骤:

  1. 移除 Undertow 依赖:

    xml 复制代码
    <!-- 删除以下依赖 -->
    <!--
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-undertow</artifactId>
    </dependency>
    -->
  2. 显式添加 Tomcat(通常无需添加,因 spring-boot-starter-web 已默认包含):

    xml 复制代码
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
  3. server.undertow.* 配置转换为 server.tomcat.*(如有必要):

    yaml 复制代码
    server:
      tomcat:
        threads:
          max: 200
          min-spare: 10
        max-connections: 8192

方案 2:改用 Jetty

Jetty 同样支持 Servlet 6.1,且在某些 I/O 密集型场景下表现优异。

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

⚠️ 注意:需排除 Tomcat 依赖,避免冲突:

xml 复制代码
<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>

四、Spring Boot 4.0 其他重要变更(简要)

除了 Undertow 移除,Spring Boot 4 还带来多项重大更新:

类别 变更内容
构建工具 Gradle 需 ≥ 8.14 或 9.x
可观测性 新增 spring-boot-starter-opentelemetry,支持 OTLP 指标与追踪
API 管理 内置 API 版本控制(Spring MVC / WebFlux)
HTTP 客户端 引入 @HttpServiceClient 简化远程调用
空值安全 支持 JSpecify 注解(@Nullable / @NonNull
虚拟线程 启用 spring.threads.virtual.enabled=true 可使用 JDK 虚拟线程提升并发

五、结语:拥抱变化,面向未来

Undertow 的退出,标志着 Spring Boot 正在加速向 云原生、可观测性、现代化 Java(JDK 21+) 演进。虽然短期带来迁移成本,但长期看,这是生态健康发展的体现。


相关推荐
止语Lab1 小时前
好的 DX 不等于少写代码——三种语言的摩擦力设计课
后端
吃饱了得干活1 小时前
别再手动解析 LLM 输出了!LangChain 四种结构化输出方案对比
后端·python·langchain
程序员天天困1 小时前
Arthas trace 命令怎么用?一行定位最慢那行代码
jvm·后端
Huiturn1 小时前
GPT 5.6 连续编码 10 小时,纯 Python 啃下 Word 二进制格式——doc2docx 实现拆解
后端
用户298698530141 小时前
Python 实现 Excel 与 Markdown 互转的实用指南
后端·python·excel
用户77283104908401 小时前
krono-job:零侵入、单二进制交付的分布式任务调度平台(开源)
后端
Conan在掘金1 小时前
鸿蒙报错速查:Function lacks ending return statement,返回路径漏 return 就炸,根因 + 真解法
后端
人间凡尔赛2 小时前
AI-Native 云原生架构:2026 年从容器编排到智能体编排的范式革命
后端·云原生·架构
IT_陈寒2 小时前
Vue的响应式让我加班到凌晨3点,原来问题出在这
前端·人工智能·后端
卷无止境2 小时前
提升 Python 代码健壮性的方法大盘点
后端·python