Java接入DeepSeek的保姆級教程(適合新手)
前言
對于目前的DeepSeek大家應該都不是很陌生,目前也是最流行的一款AI軟件了,所以為了讓我們開發(fā)更全面,能夠在自己的項目中融入AI那就會很全面了,所以這次的文章,將模擬一個基礎案例,可以在這個基礎案例迭代實現(xiàn)出你自己的AI。
話不多說,也不介紹我的網站了,直接開始進行一下流程。
使用的:JDK 17
1、獲取自己在DeepSeek上的token
網站: DeepSeek | 深度求索 ,點擊API開放平臺找到API keys 獲取自己的key,注意你的key一定要保存好了

2、引入依賴
這個就不多說了,在你的pom文件中引入相對應的依賴即可。
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>3.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<!-- HTTP客戶端 -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.0</version>
</dependency>
<!-- JSON處理 -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.4.9</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
</dependencies>3、創(chuàng)建實體類
@Data
@Builder
public class DeeseekRequest {
private String model;
private List<Message> messages;
@Data
@Builder
public static class Message {
private String role;
private String content;
}
}4、創(chuàng)建Controller層
package com.wdc.dk;
import com.google.gson.Gson;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@RestController
public class AIController {
private final Gson gson = new Gson();
@PostMapping("tall")
public String tallQuestion(@org.springframework.web.bind.annotation.RequestBody String question) throws IOException, UnirestException {
Unirest.setTimeouts(0, 0);
//DeeseekRequest: 自己的實體類名稱
List<DeeseekRequest.Message> messages = new ArrayList<>();
//給deepSeek一個角色
messages.add(DeeseekRequest.Message.builder().role("system").content("你是一個語言學家").build());
// question:說你自己想說的話
messages.add(DeeseekRequest.Message.builder().role("user").content(question).build());
DeeseekRequest requestBody = DeeseekRequest.builder()
.model("deepseek-chat")
.messages(messages)
.build();
HttpResponse<String> response = Unirest.post("https://api.deepseek.com/chat/completions")
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.header("Authorization", "Bearer "+"自己的key")
.body(gson.toJson(requestBody))
.asString();
return response.getBody();
}
}5、啟動項目、調用自己的接口

你就會發(fā)現(xiàn),你所需要的答案就會被AI回答出來,快去試試吧,像你的目標前進!
到此這篇關于Java接入DeepSeek的保姆級教程(適合新手)的文章就介紹到這了,更多相關Java接入DeepSeek內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java使用Freemarker頁面靜態(tài)化生成的實現(xiàn)
這篇文章主要介紹了Java使用Freemarker頁面靜態(tài)化生成的實現(xiàn),頁面靜態(tài)化是將原來的動態(tài)網頁改為通過靜態(tài)化技術生成的靜態(tài)網頁,FreeMarker?是一個用?Java?語言編寫的模板引擎,它基于模板來生成文本輸,更多相關內容需要的小伙伴可以參考一下2022-06-06
ByteArrayInputStream簡介和使用_動力節(jié)點Java學院整理
ByteArrayInputStream 是字節(jié)數(shù)組輸入流。它繼承于InputStream。這篇文章主要介紹了ByteArrayInputStream簡介和使用_動力節(jié)點Java學院整理,需要的朋友可以參考下2017-05-05
springboot 高版本后繼續(xù)使用log4j的完美解決方法
這篇文章主要介紹了 springboot 高版本后繼續(xù)使用log4j的解決方法,需要的朋友可以參考下2017-12-12
Java使用StopWatch輸出執(zhí)行耗時的方法詳解
StopWatch是一個簡單而強大的工具,可以幫助我們測量代碼執(zhí)行時間,從而優(yōu)化性能,這篇文章主要介紹了Java使用StopWatch輸出執(zhí)行耗時的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-07-07
Jmeter關聯(lián)實現(xiàn)及參數(shù)化使用解析
這篇文章主要介紹了Jmeter關聯(lián)實現(xiàn)及參數(shù)化使用解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08
spring cloud gateway集成hystrix全局斷路器操作
這篇文章主要介紹了spring cloud gateway集成hystrix全局斷路器操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07

