各位摸鱼工程师、SpringBoot 配置大师、AI 调参侠们,大家好!今天咱们来玩点高级的——用 SpringBoot 接入 DeepSeek,让 AI 帮你写代码、查 Bug、甚至自动回复产品经理的需求(“这个需求做不了”一键发送!)。
如果你还在手动写 CRUD,那你就 out 了!现在流行的是 “AI 生成,人类审核”,简称 “让AI打工,自己躺平”。
废话不多说,直接开搞!
1. 先搞个 SpringBoot 项目(不会的自行面壁)
如果你连 SpringBoot 项目都不会建,建议先 Google 一下《如何用 Spring Initializr 新建项目》(或者直接 IDEA 一键生成)。
依赖(pom.xml):
<dependencies>
<!-- SpringBoot Web(用来写API) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- OkHttp(HTTP客户端,比RestTemplate更香) -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.3</version>
</dependency>
<!-- Lombok(自动生成getter/setter,减少手写代码) -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
2. 配置 DeepSeek API Key(别泄露,否则AI可能替你提离职)
在 application.yml
(或 application.properties
)里加上你的 DeepSeek API Key:
deepseek:
api:
key: "your-api-key-here" # 替换成你的真实API Key
url: "https://api.deepseek.com/v1/chat/completions" # DeepSeek API地址
然后在代码里读取配置:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "deepseek.api")
@Data // Lombok自动生成getter/setter
public class DeepSeekConfig {
private String key;
private String url;
}
3. 封装 DeepSeek 客户端(让AI替你打工)
我们写个 DeepSeekClient
,专门负责和 AI 聊天:
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DeepSeekClient {
@Autowired
private DeepSeekConfig config;
private final OkHttpClient httpClient = new OkHttpClient();
public String chatWithAI(String prompt) throws Exception {
// 构造请求体(告诉AI你想干啥)
String jsonBody = String.format("{\"prompt\":\"%s\",\"max_tokens\":200}", prompt);
RequestBody body = RequestBody.create(jsonBody, MediaType.get("application/json"));
Request request = new Request.Builder()
.url(config.getUrl())
.post(body)
.addHeader("Authorization", "Bearer " + config.getKey())
.build();
// 发送请求并解析响应
try (Response response = httpClient.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new RuntimeException("AI不理你,错误码:" + response.code());
}
return response.body().string();
}
}
}
4. 写个 Controller,让前端可以调 AI
现在,我们搞个 API,让前端(或者Postman)可以发送请求给 DeepSeek:
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/ai")
public class AIController {
@Autowired
private DeepSeekClient deepSeekClient;
@PostMapping("/chat")
public String chat(@RequestBody String prompt) {
try {
return deepSeekClient.chatWithAI(prompt);
} catch (Exception e) {
return "AI罢工了,原因:" + e.getMessage();
}
}
}
5. 测试一下,让AI帮你写代码!
启动 SpringBoot 项目,用 Postman 或 curl 测试:
curl -X POST http://localhost:8080/api/ai/chat \
-H "Content-Type: application/json" \
-d "用Java写一个SpringBoot的登录接口,要求用JWT"
AI 可能返回:
{
"response": "// 1. 先加依赖:spring-boot-starter-security, jjwt\n// 2. 写JWT工具类...\n// 3. 配置Security...\n// 4. 写登录接口..."
}
恭喜!你现在可以让 AI 帮你写代码了! 🎉
6. 进阶玩法(摸鱼高级技巧)
- 自动生成 Swagger 文档:让 AI 根据代码生成 API 文档,省去手写时间。
- 自动回复产品经理:集成企业微信/钉钉,AI 自动回复“这个需求做不了”。
- 代码审查:让 AI 检查你的代码,找出潜在的 Bug(然后甩锅给AI)。
7. 可能遇到的坑(翻车指南)
✅ 403 Forbidden → 检查 API Key 是不是写错了。
✅ 慢如蜗牛 → 可能是 DeepSeek 服务器卡了,或者你的网速不行。
✅ AI 胡说八道 → 调整 prompt
,让它更明确(比如“用Java写,不要Python”)。
总结
SpringBoot + DeepSeek = “让AI写代码,自己专心摸鱼” 🤖💻
最后提醒:
- API Key 别泄露,否则 AI 可能替你提交辞职信。
- AI 写的代码记得检查,别让它偷偷引入
System.exit(0)
。 - 如果项目上线了,记得请AI喝咖啡(虽然它喝不了)。
好了,快去试试吧!摸鱼快乐! 🚀