想在Java项目里接入AI?不用写HTTP客户端,不用处理JSON解析,Spring AI让一切变得简单。
为什么你需要关注Spring AI?
你有没有算过,在Java项目里接入AI需要多少代码?
如果直接调用大模型API,你需要:
- 写HTTP客户端代码
- 处理请求/响应的JSON序列化
- 实现错误重试机制
- 处理超时控制
- 管理API Key安全
光是这些基础设施代码,就能写满一个文件。而且每次新增AI功能,都要重复这些工作。
Spring AI就是来解决这个问题的。
它把AI能力封装成Spring风格的API,你只需要像调用Bean一样使用ChatClient,剩下的交给框架处理。
这篇文章带你在5分钟内跑通第一个AI聊天程序。用DeepSeek模型,国内直接访问,价格极低。不废话,直接上手。
本文目标
读完本文,你将能够:
- ✅ 搭建Spring AI开发环境
- ✅ 编写一个可运行的AI聊天接口
- ✅ 理解ChatClient的核心用法
- ✅ 排查常见问题
准备工作
开始之前,确认你有这些:
| 组件 | 版本 | 检查方式 |
|---|---|---|
| JDK | 17+ | java -version |
| Maven | 3.6+ | mvn -version |
| DeepSeek API Key | 有效 | 注册获取 |
获取API Key
- 访问 DeepSeek开放平台
- 注册/登录后进入「API Keys」
- 点击「创建API Key」
- 复制生成的Key
设置环境变量:
# macOS / Linux
export DEEPSEEK_API_KEY=sk-xxx
# Windows PowerShell
$env:DEEPSEEK_API_KEY="sk-xxx"
为什么选DeepSeek?
| 对比项 | DeepSeek | OpenAI |
|---|---|---|
| 国内访问 | ✅ 直接访问 | ❌ 需要代理 |
| 价格 | 约1元/百万token | 约10元/百万token |
| API格式 | 兼容OpenAI | 原生 |
| 代码能力 | 强 | 强 |
DeepSeek完全兼容OpenAI的API格式,代码几乎不用改。
创建项目
项目结构
01-quick-start/
├── pom.xml
├── src/main/java/com/xalgocapital/
│ ├── QuickStartApplication.java
│ └── ChatController.java
├── src/main/resources/
│ └── application.yml
└── README.md
Maven配置
pom.xml 关键配置:
<properties>
<java.version>17</java.version>
<spring-ai.version>1.1.4</spring-ai.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- DeepSeek专用Starter -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-deepseek</artifactId>
</dependency>
</dependencies>
版本说明:
- Spring AI 1.1.4 已在Maven中央仓库发布,无需额外仓库配置
- DeepSeek专用Starter是1.1.4新增的,开箱即用
应用配置
application.yml:
server:
port: 8080
spring:
application:
name: spring-ai-quick-start
ai:
deepseek:
api-key: ${DEEPSEEK_API_KEY}
chat:
options:
model: deepseek-chat
temperature: 0.7
配置要点:
- 使用
spring.ai.deepseek前缀,无需配置base-url model用deepseek-chat,DeepSeek主力模型api-key从环境变量读取,避免硬编码
启动类
package com.xalgocapital;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class QuickStartApplication {
public static void main(String[] args) {
SpringApplication.run(QuickStartApplication.class, args);
}
}
标准的Spring Boot启动类,没什么特别的。
聊天接口
package com.xalgocapital;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.*;
@RestController
public class ChatController {
private final ChatClient chatClient;
public ChatController(ChatClient.Builder builder) {
this.chatClient = builder.build();
}
@GetMapping("/chat")
public String chat(@RequestParam(defaultValue = "你好") String message) {
return chatClient.prompt()
.user(message)
.call()
.content();
}
}
核心是ChatClient。
它封装了和AI模型交互的所有细节。调用链:
prompt() → user() → call() → content()
│ │ │ │
创建对话 添加消息 发送请求 获取回复
对比直接调用OpenAI API的代码,这简洁太多了。
运行测试
启动项目
cd 01-quick-start
mvn spring-boot:run
看到这行日志说明成功:
Started QuickStartApplication in 2.345 seconds
测试接口
默认问候:
curl http://localhost:8080/chat
响应:
你好!有什么我可以帮助你的吗?
自定义问题:
curl "http://localhost:8080/chat?message=用一句话解释什么是微服务"
响应:
微服务是一种架构风格,将应用拆分成多个小型、独立的服务,每个服务专注单一职责,可独立部署和扩展。
常见问题
问题1:依赖下载失败
现象: Maven报错 Could not find artifact org.springframework.ai...
原因: 网络问题或Maven本地缓存损坏
解决:
# 清理本地缓存后重试
mvn dependency:purge-local-repository
mvn clean install
问题2:API Key无效
现象: 启动正常,请求报错 401 Unauthorized
排查:
# 检查环境变量
echo $DEEPSEEK_API_KEY
# 如果为空,重新设置
export DEEPSEEK_API_KEY=sk-xxx
问题3:连接失败
现象: 请求报错 Connection refused 或 timeout
排查:
# 测试DeepSeek API是否可达
curl https://api.deepseek.com/v1/models \
-H "Authorization: Bearer $DEEPSEEK_API_KEY"
如果返回模型列表,说明网络正常。如果超时,检查网络环境。
背后发生了什么?
当你调用 /chat?message=你好 时:
ChatController 接收请求
│
▼
ChatClient.prompt() 创建对话上下文
│
▼
user("你好") 添加用户消息
│
▼
call() 发送到DeepSeek API
├── 自动添加model、temperature参数
├── 自动处理HTTP请求、JSON序列化
│
▼
content() 提取AI回复
│
▼
返回:"你好!有什么我可以帮助你的吗?"
你不需要关心HTTP客户端、JSON解析、错误重试——Spring AI全部帮你做了。
小结
这篇文章让你跑通了第一个AI应用:
| 你学到了 | 关键点 |
|---|---|
| 项目搭建 | BOM 依赖管理 |
| 核心配置 | 环境变量管理API Key |
| ChatClient | prompt → user → call → content |
| 问题排查 | 依赖、认证、网络 |
但这只是开始。
系列导航:
- 上一篇: 无(这是第一篇)
- 下一篇: 让AI输出更可控:Java开发者的Prompt工程实战指南
本文是「Spring AI实战入门」系列第1篇,目标帮助Java开发者快速掌握AI应用开发。