Artificial Intelligence 8 min read

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.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Integrate DeepSeek AI with Spring Boot 3: Complete Hands‑On Guide

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>&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-starter-webflux&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
  &lt;groupId&gt;io.github.pig-mesh.ai&lt;/groupId&gt;
  &lt;artifactId&gt;deepseek-spring-boot-starter&lt;/artifactId&gt;
  &lt;version&gt;1.4.5&lt;/version&gt;
&lt;/dependency&gt;</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&lt;ChatCompletionResponse&gt; chat(String prompt) {
    return deepSeekClient.chatFluxCompletion(prompt);
}</code>

Access via http://localhost:8080/chat?prompt=使用Java实现访问者模式 .

Chat streaming output
Chat streaming output

Result image.

Final answer
Final answer

4. Advanced Configuration

<code>@GetMapping(value = "/chat/advanced", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux&lt;ChatCompletionResponse&gt; 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.

SSE debugging page
SSE debugging page

7. Web Search Integration

<code>@GetMapping(value = "/search/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux&lt;ChatCompletionResponse&gt; 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 ).

AISpring BootReactiveDeepSeekJava SDKSSEWeb Search
Spring Full-Stack Practical Cases
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.