泡沫机械
96.43M · 2026-03-24
| 项目 | 内容 |
|---|---|
| 版本号 | 2.0.0-M3 |
| 发布日期 | 2026-03-17 |
| 新特性 | 23个 |
| Bug修复 | 45个 |
| 文档改进 | 16个 |
| 其他改进 | 67个 |
Spring AI 2.0.0-M3 与 Spring Boot 4.0 配套:
这是最重要的变化:MCP(Model Context Protocol)相关注解从社区包迁移到Spring AI核心。
包名变更:
| 变更前 | 变更后 |
|---|---|
org.springaicommunity.mcp.annotation.* | org.springframework.ai.mcp.annotation.* |
迁移示例:
// Before (1.x)
import org.springaicommunity.mcp.annotation.McpTool;
import org.springaicommunity.mcp.annotation.McpPrompt;
// After (2.0.0-M3)
import org.springframework.ai.mcp.annotation.McpTool;
import org.springframework.ai.mcp.annotation.McpPrompt;
Maven依赖变更:
<!-- Before: 需要单独引入 -->
<dependency>
<groupId>org.springaicommunity</groupId>
<artifactId>mcp-annotations</artifactId>
</dependency>
<!-- After: 已包含在Spring AI核心中,无需单独引入 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-annotations</artifactId>
</dependency>
Spring AI 2.0 使用 Jackson 3(tools.jackson 包),适配 Spring Boot 4。
影响:
com.fasterxml.jackson 变为 tools.jackson迁移示例:
// Before (Jackson 2)
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.JsonProperty;
// After (Jackson 3)
import tools.jackson.databind.ObjectMapper;
import tools.jackson.annotation.JsonProperty;
OpenAI集成现在使用新的Builder模式,API更流畅。
Before:
OpenAiChatOptions options = new OpenAiChatOptions();
options.setModel("gpt-4");
options.setTemperature(0.7);
options.setMaxTokens(1000);
After (2.0.0-M3):
OpenAiChatOptions options = OpenAiChatOptions.builder()
.model("gpt-4")
.temperature(0.7)
.maxTokens(1000)
.build();
以下模型选项类都统一使用Builder模式:
| 类名 | 变更 |
|---|---|
AnthropicChatOptions | 构造函数 → Builder |
AzureOpenAiChatOptions | 构造函数 → Builder |
MistralAiChatOptions | 构造函数 → Builder |
BedrockChatOptions | 构造函数 → Builder |
DeepSeekChatOptions | 构造函数 → Builder |
Claude 3 Opus、Sonnet、Haiku 已被移除,必须迁移到 Claude 4.x。
模型映射:
| 旧模型 | 新模型 |
|---|---|
claude-3-opus | claude-opus-4-6 |
claude-3-sonnet | claude-sonnet-4-6 |
claude-3-haiku | claude-haiku-4-5 |
代码迁移:
// Before
AnthropicChatOptions options = new AnthropicChatOptions();
options.setModel("claude-3-opus-20240229");
// After
AnthropicChatOptions options = AnthropicChatOptions.builder()
.model("claude-opus-4-6")
.build();
方法名更加语义化:
// Before (已废弃但保留兼容)
chatClient.disableMemory()
// After (推荐)
chatClient.disableInternalConversationHistory()
MCP Spring Transport 模块从 MCP Java SDK 迁移到 Spring AI:
| 变更前 | 变更后 |
|---|---|
io.modelcontextprotocol.sdk:mcp-spring-webflux | org.springframework.ai:mcp-spring-webflux |
io.modelcontextprotocol.sdk:mcp-spring-webmvc | org.springframework.ai:mcp-spring-webmvc |
两个接口合并为一个:
// Before: 两个独立接口
McpAsyncClientCustomizer
McpSyncClientCustomizer
// After: 统一接口
McpClientCustomizer
使用新的MCP注解定义工具:
import org.springframework.ai.mcp.annotation.McpTool;
import org.springframework.ai.mcp.annotation.McpPrompt;
import org.springframework.stereotype.Component;
@Component
public class WeatherTools {
@McpTool(description = "获取指定城市的天气信息")
public String getWeather(String city) {
// 实现天气查询逻辑
return city + " 今天晴,温度25°C";
}
@McpPrompt(description = "生成天气报告模板")
public String weatherReportPrompt(String city) {
return "请为" + city + "生成详细的天气报告";
}
}
Anthropic集成从RestClient改为官方Java SDK:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-anthropic</artifactId>
</dependency>
@Service
public class ClaudeService {
private final ChatClient chatClient;
public ClaudeService(ChatClient.Builder builder) {
this.chatClient = builder
.defaultSystem("你是一个有帮助的助手")
.build();
}
public String chat(String userMessage) {
return chatClient.prompt()
.user(userMessage)
.call()
.content();
}
}
所有ChatOptions都支持Builder模式:
// Anthropic
AnthropicChatOptions anthropicOptions = AnthropicChatOptions.builder()
.model("claude-sonnet-4-6")
.temperature(0.7)
.maxTokens(2000)
.build();
// Azure OpenAI
AzureOpenAiChatOptions azureOptions = AzureOpenAiChatOptions.builder()
.model("gpt-4")
.temperature(0.5)
.build();
// Mistral
MistralAiChatOptions mistralOptions = MistralAiChatOptions.builder()
.model("mistral-large")
.temperature(0.3)
.build();
Spring AI提供了OpenRewrite配方,可以自动完成大部分迁移工作:
mvn org.openrewrite.maven:rewrite-maven-plugin:6.32.0:run
-Drewrite.configLocation=
-Drewrite.activeRecipes=org.springframework.ai.migration.M3MigrateMcpAnnotations
-Dmaven.compiler.failOnError=false
org.springaicommunity:mcp-annotations依赖disableMemory()为disableInternalConversationHistory()<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.0.4</version>
</parent>
<properties>
<spring-ai.version>2.0.0-M3</spring-ai.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url></url>
</repository>
</repositories>
| 组件 | 1.x版本 | 2.0.0-M3 |
|---|---|---|
| Spring Boot | 3.4.x / 3.5.x | 4.0.x |
| Spring Framework | 6.x | 7.0 |
| Java | 17+ | 21+ |
| Jackson | 2.x | 3.x |
| MCP SDK | 0.18.x | 1.0.x |
Spring AI 2.0.0-M3 是一个重要的里程碑版本:
| 变化类型 | 影响 |
|---|---|
| MCP核心集成 | 降级使用门槛,官方支持 |
| Jackson 3迁移 | 与Spring Boot 4保持一致 |
| Builder模式统一 | API更一致、更流畅 |
| Claude 4.x支持 | 使用最新模型能力 |
升级建议: