spring aop maven Spring Boot 4 + Spring AI + DeepSeek V4:Maven项目一键接入AI
聚焦( Boot 4、 AI、 V4)三大核心组件的快速整合,适配最新稳定版本,无需复杂配置,复制粘贴即可运行,适用于个人开发、中小项目快速接入AI能力。
一、核心简介
1. Boot 4
2. AI
3. V4
二、环境要求(必须...
聚焦( Boot 4、 AI、 V4)三大核心组件的快速整合,适配最新稳定版本,无需复杂配置,复制粘贴即可运行,适用于个人开发、中小项目快速接入AI能力。

一、核心简介
1. Boot 4
2. AI
3. V4
二、环境要求(必须一致)
三、第一步:获取 API Key
打开:登录 → 左侧API Keys点击 API Key → 复制保存(sk-xxxx)新用户有免费额度,可直接测试V4

四、第二步:创建项目&Maven依赖
pom.xml 完整配置
4.0.0
org.springframework.boot
spring-boot-starter-parent
4.0.0
com.example
deepseek-ai-demo
0.0.1-SNAPSHOT
17
1.1.5
org.springframework.ai
spring-ai-bom
${spring-ai.version}
pom
import
org.springframework.boot
spring-boot-starter-web
org.springframework.ai
spring-ai-starter-model-deepseek
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-maven-plugin
五、第三步:配置文件
server:
port: 8080
spring:
ai:
deepseek:
api-key: sk-你自己的APIKey
base-url: https://api.deepseek.com
chat:
options:
# 二选一:
model: deepseek-v4-flash # 快速版(推荐)
# model: deepseek-v4-pro # 专业版
temperature: 0.7 # 创意度 0~1
max-tokens: 4096 # 最大输出长度
六、第四步:核心代码
1. 启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DeepSeekAiApplication {
public static void main(String[] args) {
SpringApplication.run(DeepSeekAiApplication.class, args);
}
}
2. AI对话接口
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
@RestController
@RequestMapping("/ai")
public class DeepSeekController {
private final ChatClient chatClient;
// 自动注入 DeepSeek 模型
public DeepSeekController(ChatModel chatModel) {
this.chatClient = ChatClient.create(chatModel);
}
/**
* 同步对话(一次性返回结果)
*/
@PostMapping("/chat")
public String chat(@RequestBody String message) {
return chatClient.prompt(message).call().content();
}
/**
* 流式对话(SSE 打字机效果,适合长文本)
*/
@PostMapping(value = "/stream", produces = "text/event-stream")
public Flux streamChat(@RequestBody String message) {
return chatClient.prompt(message).stream().content();
}
}
七、第五步:启动&测试
1. 启动项目
DeepSeekAiApplication 启动成功
2. 接口测试
(1)同步接口
curl -X POST http://localhost:8080/ai/chat \
-H "Content-Type: application/json" \
-d "你好,介绍一下 DeepSeek V4"
(2)流式接口(推荐)
curl -X POST http://localhost:8080/ai/stream \
-H "Content-Type: application/json" \
-d "写一篇300字的Spring AI介绍"
八、 V4 模型说明(关键)

个人开发首选:flash
九、常见问题(100% 避坑)
401 未授权:API Key错误/未充值/过期model not found:模型名写错:必须是-v4-flash或-v4-pro启动报错:找不到:检查yml配置:api-key和base-url不能为空依赖下载失败:网络问题,使用国内镜像源
十、扩展能力( AI强大之处)
你可以在此基础上轻松实现:
总结
这是 Boot 4、 AI与 V4的最新稳定整合方案,流程极简、无冗余配置,核心步骤清晰,避开常见坑点,适合快速上手接入AI能力,无论是个人学习还是项目开发,均可直接套用。
























