跪拜 Guibai
← All articles
Backend · Java · AI Programming

Spring Boot 4 + Spring AI 2.0: Count the Migration Debt Before You Touch the Version Number

By Rain的Java大神之路 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Teams that treat Spring AI 2.0 as a version bump will ship silent breakage: JSON contracts change without compile errors, tool calls can execute twice or stop after the first response, and chat memory order quietly degrades. The migration is a platform-level debt project, and the difference between success and failure is contract tests, rollback paths, and token cost visibility.

Summary

Spring AI 2.0's baseline is Boot 4.0/4.1 + Spring Framework 7, and Spring's own line is that it cannot be loaded in a 3.x context. The recommended landing point is JDK 21 + Boot 4.1 + Spring AI 2.0.x, skipping Boot 4.0 and avoiding a two-major-version jump. The heavy work sits below the AI layer: javax-to-jakarta conversion can silently disable auto-configuration through stale @ConditionalOnClass checks; Jackson 3 moves packages to tools.jackson, turns exceptions unchecked, and flips six serialization defaults, including dates changing from timestamps to ISO-8601 strings. Boot 4's starter modularization removes implicit autoconfiguration, Undertow is gone, virtual threads are on by default, and @MockBean becomes @MockitoBean.

Spring AI 2.0 then rewrites the AI contract: ChatClient is the user-facing API while ChatModel drops to a building block; Options become immutable builders; tool calling moves into a ToolCallingAdvisor chain where a second advisor duplicates executions; MCP defaults switch from SSE to Streamable HTTP; provider facades are replaced by official SDKs; and Anthropic's maxTokens default jumps from 500 to 4096. JDBC ChatMemory needs a sequence_id column, and the migration path is expand, backfill, validate, switch.

The recommended route is either a four-stage monolith upgrade (JDK first, then Boot 3.5 to 4.1, then AI 1.x to 2.0) or a standalone Boot 4.1 AI gateway service that old services call over HTTP. Acceptance is behavioral equivalence: contract snapshots, tool-call roundtrip assertions, ChatMemory session tests, RAG eval sets, shadow-to-canary rollout, and rollback drills. Estimated effort for a 20-module service with AI and internal starters is 3-4 person-weeks for platform plus 1-2 for AI adaptation, with 60% of time spent verifying rather than editing code.

Takeaways
— Spring AI 2.0 requires Spring Boot 4.0/4.1 and Spring Framework 7; it cannot load in a Boot 3.x classpath.
— The recommended target is JDK 21 + Boot 4.1 + Spring AI 2.0.x. Boot 4.0 OSS support ends 2026-12-31; Boot 4.1 runs until 2027-07-31. Boot 4 still accepts Java 17.
— Stale javax-based @ConditionalOnClass annotations do not error; they silently disable auto-configuration while the service still starts and reports healthy.
— Jackson 3 moves the core package from com.fasterxml.jackson to tools.jackson, but jackson-annotations stays in com.fasterxml.jackson.
— Jackson 3 exceptions are unchecked, so existing catch (IOException) fallback blocks compile but never run. Six serialization defaults flip, most notably WRITE_DATES_AS_TIMESTAMPS from enabled to disabled.
— Boot 4 splits spring-boot-autoconfigure into focused modules; spring-boot-starter-web becomes spring-boot-starter-webmvc and Flyway/Liquibase need explicit starters.
— The official spring-boot-starter-classic bridge can get a green build during migration, but needs a removal deadline or it becomes permanent architecture.
— Custom starters must move auto-configuration registration from META-INF/spring.factories to AutoConfiguration.imports, and internal auto-configuration classes are no longer safe to subclass.
— Virtual threads are on by default. Large platform thread pools lose meaning, ThreadLocal context can be lost across parallel @Tool calls, and synchronized blocking I/O can pin carrier threads.
— Test annotations change from @MockBean/@SpyBean to @MockitoBean/@MockitoSpyBean; @AutoConfigureMockMvc must be explicit and Undertow is no longer an option.
— Spring AI 2.0 makes ChatClient the user-facing API, ChatModel a lower-level block, Options immutable builder objects, and ChatClient.options() accepts a Builder only.
— Tool calling moves into ToolCallingAdvisor. Only one ToolAdvisor is allowed per chain; manual registration duplicates tool execution. Direct ChatModel.call() returns only the first tool-call response.
— MCP transport defaults to Streamable HTTP instead of SSE; package names move from org.springaicommunity.mcp.annotation to org.springframework.ai.mcp.annotation.
— Anthropic maxTokens default changes from 500 to 4096, so services relying on the old cap can burn API quota overnight.
— JDBC ChatMemory adds sequence_id; migrate with expand, backfill, validate, switch rather than rebuilding the table.
— A 20-module service with AI chain and internal starters costs about 3-4 person-weeks for platform migration and 1-2 person-weeks for AI adaptation; 60% of that is verification.
Conclusions

The dangerous failures in this migration are all silent: auto-configuration that stops loading, Jackson exceptions that never reach the old catch, JSON dates that change shape, a second tool advisor that doubles execution. Contract snapshots and /actuator/conditions diffs are the right response to that pattern.

Spring AI 2.0's visible API changes are smaller than the platform work underneath it. A team that budgets for an AI upgrade but not for Boot 4 will misestimate by weeks.

Moving tool calling into an advisor chain is a genuine improvement: guardrails like token budgets and round limits become pluggable. The new duplicate-execution footgun shows how a central abstraction can create its own failure mode.

The Anthropic maxTokens default change from 500 to 4096 is a quiet cost explosion. Provider SDK abstraction changes carry financial risk, not just compile risk.

The AI gateway route trades a risky full-platform migration for a new service with its own blast radius, which fits AI workloads' separate scaling and quota needs.

Concepts & terms
JSpecify @NullMarked
A Java null-safety annotation scheme. Boot 4 and Spring AI 2.0 treat API parameters and return values as non-null by default; nullable values must be explicitly marked @Nullable. Applying @NullMarked to a package turns nullability into a compile-time checked contract.
ToolCallingAdvisor
In Spring AI 2.0, the component that owns the model tool-call loop. It sits on the ChatClient advisor chain, auto-registers tools, and lets teams insert guardrails like round limits, token budgets, and audit hooks.
@ConditionalOnClass silent failure
When a Spring Boot condition references a class that no longer exists after a migration, the condition evaluates false and auto-configuration quietly does not load. The application starts and health checks pass, but the feature is absent.
Virtual thread pinning
When a virtual thread enters a synchronized block and blocks on I/O, the underlying platform carrier thread can become pinned, preventing it from handling other virtual threads. In Java 24+ most pinning issues are fixed; before that, -Djdk.tracePinnedThreads=full reveals it.
Streamable HTTP (MCP)
The default transport for Model Context Protocol in Spring AI 2.0, replacing SSE. It sends a normal HTTP request and streams responses back, reducing the need for long-lived event-stream connections.
OpenRewrite
An automated source-code refactoring tool. Spring AI and Spring Boot provide migration recipes that rewrite imports, method calls, and dependency coordinates mechanically, though the results still need manual review.
Session API (Spring AI 2.1 direction)
A proposed replacement for ChatMemory/MessageChatMemoryAdvisor that models a conversation as an append-only event log instead of a flat message list, enabling context compaction and recall.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗