李锋镝的博客

  • 首页
  • 时间轴
  • 评论区显眼包🔥
  • 左邻右舍
  • 博友圈
  • 关于我
    • 关于我
    • 另一个网站
    • 我的导航站
    • 网站地图
    • 赞助
  • 留言
  • 🚇开往
Destiny
自是人生长恨水长东
  1. 首页
  2. 原创
  3. 正文

Springboot接入DeepSeek API

2025年2月16日 153点热度 0人点赞 0条评论

1. 准备工作

注册并获取 API Key:你需要在 DeepSeek 平台注册账号,并获取 API Key,这是调用 API 的凭证。
创建 Spring Boot 项目:可以使用 Spring Initializr(https://start.spring.io/)创建一个新的 Spring Boot 项目,添加 Spring Web 依赖。

2. 添加依赖

在 pom.xml 中添加必要的依赖,这里使用 OkHttp 作为 HTTP 客户端来发送请求:

<dependencies>
    <!-- Spring Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- OkHttp -->
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>4.9.3</version>
    </dependency>
    <!-- JSON 处理 -->
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.8</version>
    </dependency>
</dependencies>

3. 编写配置类

创建一个配置类来存储 DeepSeek 的 API 相关信息,如 API Key 和 API 地址:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DeepSeekConfig {
    @Value("${deepseek.api.key}")
    private String apiKey;

    @Value("${deepseek.api.url}")
    private String apiUrl;

    public String getApiKey() {
        return apiKey;
    }

    public String getApiUrl() {
        return apiUrl;
    }
}

4. 编写服务类

创建一个服务类来封装调用 DeepSeek API 的逻辑:

import com.google.gson.Gson;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class DeepSeekService {
    private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
    private final OkHttpClient client = new OkHttpClient();
    private final Gson gson = new Gson();

    @Autowired
    private DeepSeekConfig config;

    public String callDeepSeekApi(String prompt) throws IOException {
        // 构建请求体
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("prompt", prompt);
        // 这里可以根据 API 文档添加更多参数,如 temperature、max_tokens 等
        requestBody.put("temperature", 0.7);
        requestBody.put("max_tokens", 200);

        String json = gson.toJson(requestBody);
        RequestBody body = RequestBody.create(json, JSON);

        // 构建请求
        Request request = new Request.Builder()
               .url(config.getApiUrl())
               .addHeader("Authorization", "Bearer " + config.getApiKey())
               .post(body)
               .build();

        // 发送请求并获取响应
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
            return response.body().string();
        }
    }
}

5. 编写控制器类

创建一个控制器类来处理客户端的请求,并调用 DeepSeek 服务:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class DeepSeekController {
    @Autowired
    private DeepSeekService deepSeekService;

    @GetMapping("/deepseek")
    public String callDeepSeek(@RequestParam String prompt) {
        try {
            return deepSeekService.callDeepSeekApi(prompt);
        } catch (IOException e) {
            e.printStackTrace();
            return "Error calling DeepSeek API: " + e.getMessage();
        }
    }
}

6. 配置文件

在 application.properties 中添加 DeepSeek 的 API 相关配置:

deepseek.api.key=your_api_key
deepseek.api.url=your_api_url

请将 your_api_key 替换为你实际的 API Key,your_api_url 替换为 DeepSeek 提供的 API 地址。

7. 测试

启动 Spring Boot 应用程序,访问 http://localhost:8080/deepseek?prompt=生成一个 Java 方法,用于计算两个整数的和,你将得到 DeepSeek 返回的响应结果。
以上示例代码展示了如何在 Spring Boot 项目中接入 DeepSeek Coder 的 API,你可以根据实际需求调整请求参数和处理逻辑。

除非注明,否则均为李锋镝的博客原创文章,转载必须以链接形式标明本文链接

本文链接:https://www.lifengdi.com/article/4194

相关文章

  • 本地部署 DeepSeek 模型并进行 Spring Boot 整合
  • DeepSeek本地部署全攻略:从环境搭建到高级应用,打造专属 AI 助手
  • 从入门到精通:Ollama 本地大模型全攻略(含 UI 界面、多语言调用、进阶优化)
  • SpringBoot常用注解
  • CompletableFuture使用详解
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: AI DeepSeek
最后更新:2025年2月16日

李锋镝

既然选择了远方,便只顾风雨兼程。

打赏 点赞
< 上一篇
下一篇 >

文章评论

1 2 3 4 5 6 7 8 9 11 12 13 14 15 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 46 47 48 49 50 51 52 53 54 55 57 58 60 61 62 63 64 65 66 67 69 72 74 76 77 78 79 80 81 82 85 86 87 90 92 93 94 95 96 97 98 99
取消回复

秋天是倒放的春天,晚安是爱你的序篇。

那年今日(02月10日)

  • 1953年:穆罕默德·纳吉布出任埃及总统
  • 1923年:德国物理学家、X射线发现者伦琴逝世
  • 1898年:德国戏剧家贝尔托·布莱希特出生
  • 1894年:英国政治家哈罗德·麦克米伦出生
  • 589年:杨坚灭陈朝,南北朝结束
  • 更多历史事件
最新 热点 随机
最新 热点 随机
Apollo配置中心中的protalDB的作用是什么 org.apache.ibatis.plugin.Interceptor类详细介绍及使用 JDK25模块级导入深度解析:Java导入机制的革命性进化 AI时代,个人技术博客的出路在哪里? 什么是Meta Server? 千万级大表新增字段实战指南:告别锁表与业务中断
玩博客的人是不是越来越少了?AI时代,个人技术博客的出路在哪里?准备入手个亚太的ECS,友友们有什么建议吗?使用WireGuard在Ubuntu 24.04系统搭建VPNWordPress实现用户评论等级排行榜插件WordPress网站换了个字体,差点儿把样式换崩了
JWT、Cookie、Session、Token 区别与实战选型指南 Spring Boot 2.5.0重新设计的spring.sql.init 配置有啥用? 微服务的数据库设计 MySQL数据库详解——执行SQL更新时,其底层经历了哪些操作? AI重构开发者工作范式:从Anthropic内部调研看Claude对研发领域的深层影响 使用Spring MVC的websocket配置时 Tomcat启动报错
标签聚合
Spring K8s docker JAVA JVM 分布式 数据库 SpringBoot AI IDEA Redis 日常 AI编程 MySQL 多线程 SQL 设计模式 WordPress ElasticSearch 架构
友情链接
  • Blogs·CN
  • Honesty
  • Mr.Sun的博客
  • 临窗旋墨
  • 哥斯拉
  • 彬红茶日记
  • 志文工作室
  • 懋和道人
  • 搬砖日记
  • 旧时繁华
  • 林羽凡
  • 瓦匠个人小站
  • 皮皮社
  • 知向前端
  • 蜗牛工作室
  • 韩小韩博客
  • 风渡言

COPYRIGHT © 2026 lifengdi.com. ALL RIGHTS RESERVED.

域名年龄

Theme Kratos Made By Dylan

津ICP备2024022503号-3

京公网安备11011502039375号