A New Collection of Thoughtful Learning Apps — Now Available on iOS & Android

Image
I’m excited to share a set of mobile apps I’ve recently completed and published on both the Google Play Store and the Apple App Store. These apps are designed with a simple goal in mind: to make meaningful, structured content more accessible, whether you’re studying theology or improving your English vocabulary. 📱 Now Available on Both Platforms All apps are live and available for download: Google Play Developer Page: https://play.google.com/store/apps/dev?id=5835943159853189043 Apple App Store Developer Page: https://apps.apple.com/ca/developer/q-z-l-corp/id1888794100 📖 Theology & Confession Study Apps For those interested in Reformed theology and classical Christian teachings, I’ve developed a series of apps that present foundational texts in a clean, focused reading format: The Belgic Confession Canons of Dort Heidelberg Catechism Westminster Shorter Catechism Each app is designed to provide a distraction-free experience, making it easier to read, reflect, and revisit these im...

Spring Boot 4 升级踩坑指南:Java 21、Jakarta、Jackson 3 和 Copilot 自动升级

Spring Boot 4 升级踩坑指南:Java 21、Jakarta、Jackson 3 和 Copilot 自动升级



Spring Boot 4 已经发布,很多团队开始计划将系统升级到 Java 21Spring Boot 4.0.3

然而在实际升级过程中,开发者会遇到很多问题,例如:

  • Jakarta 包迁移导致编译失败
  • Jackson 3 导致 JSON 反序列化变化
  • Spring Cloud Gateway 配置结构变化
  • 测试依赖和注解迁移
  • Observability / Micrometer 监控异常

本文总结了一套 企业级 Spring Boot 4 升级流程, 并介绍如何使用 GitHub Copilot Agent 自动生成升级 PR。


目录

  1. 为什么升级 Spring Boot 4
  2. 升级总体策略
  3. 使用 Copilot 自动升级
  4. Java 21 升级
  5. Spring Boot 4 升级
  6. Jakarta 迁移
  7. Jackson 3 变化
  8. Spring Cloud Gateway 配置变化
  9. 常见踩坑
  10. 升级验证 Checklist

为什么升级 Spring Boot 4

Spring Boot 4 带来了多个重要变化:

  • 默认支持 Java 21
  • Jakarta API 全面替换 javax
  • Jackson 3 JSON 生态升级
  • Micrometer Observability 改进
  • Spring Security 现代 DSL

对于长期维护的系统来说,升级可以获得:

  • 更好的性能
  • 更安全的依赖
  • 更好的云原生支持

升级总体策略

升级应该按阶段进行:

  1. 升级 Java
  2. 升级 Spring Boot
  3. 进行 Jakarta 迁移
  4. 依赖版本对齐
  5. 修复配置变化
  6. 修复测试
  7. 验证运行

每一步都必须 编译 + 测试 + 启动验证


使用 Copilot 自动升级

对于大型代码库,手动升级非常耗时。

GitHub Copilot Agent 可以自动分析项目结构, 并生成完整升级 Pull Request。

推荐 Prompt

Goal:
Upgrade this project to Java 21 and Spring Boot 4.0.3.

Constraints:
- Preserve existing behaviour
- Do NOT remove business logic
- Ensure project builds and tests pass

Tasks:
1 Upgrade Java version
2 Upgrade Spring Boot parent
3 Replace javax with jakarta
4 Align dependencies
5 Update configuration
6 Fix tests
7 Verify application startup
8 Provide migration summary

Java 21 升级

修改 Maven 配置:

<properties>
  <java.version>21</java.version>
</properties>

验证:

  • 项目可以编译
  • 单元测试通过
  • 应用可以启动

Spring Boot 4 升级

更新 parent:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.0.3</version>
</parent>

升级原则:

  • 删除 Boot 已管理的依赖版本
  • 使用 Boot starter
  • 避免手动覆盖版本

Jakarta 迁移

Spring Boot 4 已完全使用 Jakarta API。

旧包 新包
javax.servlet jakarta.servlet
javax.validation jakarta.validation
javax.persistence jakarta.persistence

IDE 通常可以自动完成 import 替换。


Jackson 3 变化

Spring Boot 4 默认使用 Jackson 3

重要变化:

  • groupId 从 com.fasterxml.jackson 变为 tools.jackson
  • @JsonComponent 改为 @JacksonComponent
  • JSON 配置迁移到 spring.jackson.json

如果暂时无法升级 Jackson 3,可以使用:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-jackson2</artifactId>
</dependency>

Spring Cloud Gateway 配置变化

Spring Cloud Gateway 在新版本中调整了配置结构。

新的 starter:

  • spring-cloud-starter-gateway-server-webflux
  • spring-cloud-starter-gateway-server-webmvc

新的配置结构:

spring:
  cloud:
    gateway:
      server:
        webflux:
          routes:

常见升级踩坑

1 Jackson 反序列化异常

Jackson 3 对 JSON 解析行为有变化, 部分旧代码会出现反序列化错误。

2 WebTestClient 注解找不到

需要增加依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux-test</artifactId>
<scope>test</scope>
</dependency>

3 Micrometer AssertionError

Reactive 应用中如果覆盖 Reactor Context, 会导致 Observability AssertionError。

解决方法:

合并 context,而不是替换。


升级验证 Checklist

升级完成后必须检查:

  • 应用成功启动
  • Health endpoint 正常
  • 认证授权正常
  • OAuth Token 流程正常
  • 外部 API 调用正常
  • 日志包含 traceId
  • Micrometer 指标正常
  • 集成测试通过

总结

Spring Boot 4 升级的难点主要在:

  • Jakarta 包迁移
  • Jackson 3 变化
  • Spring Cloud 配置变化
  • 测试依赖更新

使用 GitHub Copilot Agent 可以显著减少升级工作量, 甚至自动生成升级 Pull Request。

如果按照本文的分阶段策略升级, 可以在保证系统稳定的前提下顺利迁移到 Java 21 与 Spring Boot 4。

❤️ Support This Blog


If this post helped you, you can support my writing with a small donation. Thank you for reading.


Comments

Popular Posts

Fix “A problem occurred starting process 'command node'” in Android Studio for React Native

Fix up watchman issue with Ghost

Using Mutual TLS (mTLS) in Next.js (Server-Side Only)