java后端deepseek api如何调用
时间: 2025-07-01 07:03:23 浏览: 7
### 调用 DeepSeek API 的方法
为了在 Java 后端环境中调用 DeepSeek API,可以采用 HTTP 客户端库来发送请求并处理响应。下面是一个基于 `HttpClient` 和 `Gson` 库实现的例子:
#### 添加依赖项
如果使用 Maven 构建项目,则需向项目的 pom.xml 文件中加入如下依赖项:
```xml
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
```
#### 发送 GET 请求获取数据
这里展示了一个简单的例子,用于同步来自 SAP SuccessFactors 的人才资料信息。
```java
import com.google.gson.Gson;
import java.net.URI;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
public class ApiClient {
private static final String ENDPOINT = "https://api.successfactors.com";
private static final String API_KEY = "your_api_key";
public TalentProfile[] syncTalentProfiles() throws Exception {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
URI uri = new URI(ENDPOINT + "/talent/profiles");
HttpGet request = new HttpGet(uri);
request.setHeader("Authorization", "Bearer " + API_KEY);
try (CloseableHttpResponse response = httpClient.execute(request)) {
int statusCode = response.getCode();
if (statusCode >= 200 && statusCode < 300) {
Gson gson = new Gson();
// 假设返回的是 JSON 数组形式的人才配置文件列表
return gson.fromJson(EntityUtils.toString(response.getEntity()), TalentProfile[].class);
} else {
throw new RuntimeException("Failed : HTTP error code : "
+ statusCode);
}
}
}
}
}
```
此代码片段展示了如何创建一个名为 `ApiClient` 的类,在其中定义了 `syncTalentProfiles()` 方法用来执行对特定 URL 地址发起的 GET 请求,并解析服务器返回的数据为对象数组的形式[^2]。
请注意实际应用时应当替换掉 `"your_api_key"` 及其他占位符参数以匹配具体环境设置。
阅读全文
相关推荐


















