Integrate DeepSeek AI with Spring Boot 3: Complete Hands‑On Guide
This article introduces deepseek4j, a Java SDK for DeepSeek AI models, walks through Maven setup, Spring Boot configuration, basic and advanced usage examples—including streaming, synchronous, SSE debugging, and web‑search integration—while detailing key configuration options and best‑practice notes.
Introduction
deepseek4j is a Java SDK for DeepSeek models (R1, V3) providing chat, function calling, JSON output, and embedding generation. It offers a Spring Boot Starter for easy integration with Spring Boot 2.x/3.x and other Java web frameworks.
Features
Full DeepSeek API support, including chain‑of‑thought and billing information.
Customizable connection parameters, proxy, timeout, and logging.
Reactive (Reactor) support for streaming responses.
Getting Started
1. Environment Preparation
<code><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>io.github.pig-mesh.ai</groupId>
<artifactId>deepseek-spring-boot-starter</artifactId>
<version>1.4.5</version>
</dependency></code>2. Basic Configuration
<code>deepseek:
api-key: sk-xxxooo
model: deepseek-reasoner
base-url: https://api.deepseek.com</code>3. Basic Usage
Example controller using streaming chat:
<code>private final DeepSeekClient deepSeekClient;
public ChatController(DeepSeekClient deepSeekClient) {
this.deepSeekClient = deepSeekClient;
}
@GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ChatCompletionResponse> chat(String prompt) {
return deepSeekClient.chatFluxCompletion(prompt);
}</code>Access via http://localhost:8080/chat?prompt=使用Java实现访问者模式 .
Result image.
4. Advanced Configuration
<code>@GetMapping(value = "/chat/advanced", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ChatCompletionResponse> chatAdvanced(String prompt) {
ChatCompletionRequest request = ChatCompletionRequest.builder()
.model(ChatCompletionModel.DEEPSEEK_REASONER)
.addUserMessage(prompt)
.maxCompletionTokens(1000)
.build();
return deepSeekClient.chatFluxCompletion(request);
}</code>5. Synchronous Call (Not Recommended)
<code>@Resource
private DeepSeekProperties deepSeekProperties;
@GetMapping("/sync/chat")
public ChatCompletionResponse syncChat(String prompt) {
ChatCompletionRequest request = ChatCompletionRequest.builder()
.model(deepSeekProperties.getModel())
.addUserMessage(prompt)
.build();
return deepSeekClient.chatCompletion(request).execute();
}</code>Note: Synchronous blocking calls may cause time‑outs with the R1 model and degrade user experience.
6. SSE Debug Page
The starter also provides an SSE‑based debugging page built with Vue 3. The sse.html can be copied into your project for quick testing.
7. Web Search Integration
<code>@GetMapping(value = "/search/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ChatCompletionResponse> chatV3(String prompt) {
SearchRequest searchRequest = SearchRequest.builder()
.enable(true)
.freshness(FreshnessEnums.ONE_DAY)
.summary(true)
.count(10)
.page(1)
.build();
return deepSeekClient.chatSearchCompletion(prompt, searchRequest);
}</code>Additional configuration for the search API key:
<code>deepseek:
search-api-key: sk-xxxooo</code>Search uses the BochaAI service; obtain an API key from the provider.
https://open.bochaai.com/api-keys
Configuration Reference
Key settings include deepseek.base-url , deepseek.api-key , deepseek.model , logging flags ( deepseek.log-requests , deepseek.log-responses , deepseek.log-level ), and timeout parameters ( deepseek.connect-timeout , deepseek.read-timeout , deepseek.call-timeout ).
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.