Getting API key First, get the API key from the DeepSeek platform for authentication.
Add HTTP client dependencies to use Java's HTTP client libraries such as Apache HttpClient or OkHttp to send HTTP requests. If using Maven, you can add dependencies to:
<!-- Apache HttpClient --> <dependency> <groupId></groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency> <!-- OkHttp --> <dependency> <groupId>.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.9.3</version> </dependency>
Create HTTP requests Use the HTTP client library to create requests, set request headers, URLs, and request bodies
Example using Apache HttpClient:
import ; import ; import ; import ; import ; import ; import ; public class DeepSeekClient { private static final String API_URL = "/v1/your-endpoint"; private static final String API_KEY = "your-api-key"; public static void main(String[] args) { try (CloseableHttpClient httpClient = ()) { HttpPost httpPost = new HttpPost(API_URL); ("Authorization", "Bearer " + API_KEY); ("Content-Type", "application/json"); String json = "{\"name\":\"tom\"}"; // Replace with the actual request body (new StringEntity(json)); try (CloseableHttpResponse response = (httpPost)) { HttpEntity entity = (); if (entity != null) { String result = (entity); (result); } } } catch (Exception e) { (); } } }
Example using OkHttp
import okhttp3.*; import ; public class DeepSeekClient { private static final String API_URL = "/v1/your-endpoint"; private static final String API_KEY = "your-api-key"; public static void main(String[] args) { OkHttpClient client = new OkHttpClient(); MediaType mediaType = ("application/json"); String json = "{\"name\":\"tom\"}"; // Replace with the actual request body RequestBody body = (mediaType, json); Request request = new () .url(API_URL) .post(body) .addHeader("Authorization", "Bearer " + API_KEY) .addHeader("Content-Type", "application/json") .build(); try (Response response = (request).execute()) { if (() && () != null) { (().string()); } } catch (IOException e) { (); } } }
Through the above steps, you can successfully connect to the DeepSeek API in Java, or integrate springboot, and send requests to deepseek through springboot.
Summarize
This is the article about completing basic dialogues with JAVA calling Deepseek API. For more information about JAVA calling Deepseek API content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!