How to Build AI-Powered Java Apps with Spring AI and DeepSeek

This guide walks Java developers through integrating Spring AI with large‑model services such as DeepSeek, covering setup, API key configuration, code examples for synchronous and streaming calls, reactive implementation, monitoring with Actuator, and compatibility with OpenAI‑style APIs.

Alibaba Cloud Developer
Alibaba Cloud Developer
Alibaba Cloud Developer
How to Build AI-Powered Java Apps with Spring AI and DeepSeek

Spring AI Overview – Your AI Butler

Spring AI is an application framework that brings Spring’s portable, modular design principles to AI, simplifying integration of enterprise data or API services with large‑model providers by wrapping model calls in ordinary Java objects.

Exploring DeepSeek – From Manual to Automatic

Obtain an API key from the DeepSeek platform, then use curl to test the model:

curl https://api.deepseek.com/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <DeepSeek API Key>" \
  -d '{
    "model": "deepseek-chat",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "讲一个笑话"}
    ],
    "stream": false
  }'

The response returns a JSON object containing the generated joke.

Create a Spring Boot project (Spring Boot 3.x, JDK 17+) with the spring-ai-openai-starter dependency, which also works with DeepSeek. Configure application.properties:

spring.application.name=hello
spring.ai.openai.api-key=<your DeepSeek API key>
spring.ai.openai.base-url=https://api.deepseek.com
spring.ai.openai.chat.options.model=deepseek-chat

Implement a controller:

@RestController
public class HelloController {
    private final ChatClient chatClient;
    public HelloController(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }
    @GetMapping("/hello")
    public String hello(@RequestParam(value = "input", defaultValue = "讲一个笑话") String input) {
        return chatClient.prompt(input).call().content();
    }
}

Run the application and access http://localhost:8080/hello to see the AI‑generated joke.

Streaming Responses – Typewriter Effect

Use Reactor to emit incremental tokens for a typing‑like experience:

private ExecutorService executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MICROSECONDS, new LinkedBlockingQueue<>(10));
@GetMapping(value = "/mock/stream", produces = "text/html;charset=UTF-8")
public Flux<String> mockStream() {
    Sinks.Many<String> sink = Sinks.many().multicast().onBackpressureBuffer();
    executor.submit(() -> {
        for (int i = 0; i < 100; i++) {
            sink.tryEmitNext(i + " ");
            try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); }
        }
    });
    return sink.asFlux();
}

For AI streaming, replace .call() with .stream():

@GetMapping(value = "/hello/stream", produces = "text/html;charset=UTF-8")
public Flux<String> helloStream(@RequestParam(value = "input", defaultValue = "讲一个笑话") String input) {
    return chatClient.prompt(input).stream().content();
}

OpenAI Starter Compatibility with DeepSeek

The spring-ai-openai-starter uses the OpenAI API contract, which DeepSeek and other Chinese providers also implement. By setting spring.ai.openai.base-url to the provider’s endpoint and supplying the appropriate API key, the same code works across models, similar to JDBC’s driver abstraction.

Production Monitoring for AI

Add Actuator and Micrometer Prometheus dependencies:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

Enable monitoring:

management.endpoints.web.exposure.include=health,metrics,prometheus
spring.ai.observability.enabled=true

Visit /actuator/prometheus to view metrics, which can be visualized with Grafana.

Conclusion

Spring AI provides Java developers with a unified foundation for building enterprise‑grade AI applications, supporting multiple large‑model providers, reactive streaming, and production‑ready observability, making the AI era accessible from the familiar Spring ecosystem.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavamonitoringDeepSeekspring-aiAI integrationReactive streaming
Alibaba Cloud Developer
Written by

Alibaba Cloud Developer

Alibaba's official tech channel, featuring all of its technology innovations.

0 followers
Reader feedback

How this landed with the community

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.