Run Your First Spring AI 2.0 Conversation in 5 Minutes
This article introduces Spring AI 2.0, explains why Java developers should adopt it, and walks through setting up a Spring Boot 3.x project with JDK 17, adding the DeepSeek starter, configuring properties, writing a simple ChatController, and running a curl request to see the model’s reply.
Spring AI provides a Spring‑style façade ( ChatClient) for invoking large language models, analogous to how RestTemplate and WebClient wrap HTTP calls.
Instead of manually constructing JSON, sending HTTP requests, and parsing responses, a model call reduces to a single fluent line: chatClient.prompt(msg).call() .
Version 2.0 modularization
The previous monolithic jar is split into independent modules such as spring-ai-client-chat, spring-ai-vector-store, and spring-ai-rag. For a simple conversation only the DeepSeek starter is required.
Prerequisites
JDK 17+ – required by Spring Boot 3.x, which Spring AI 2.0 runs on.
DeepSeek API Key – obtain from platform.deepseek.com and fund the account.
The same code works with OpenAI, Tongyi, or other providers by swapping the starter and adjusting configuration.
Project setup
Create a Spring Boot 3.x project with Spring Initializr ( start.spring.io) and enable Spring Web. Then add the Spring AI BOM and the DeepSeek starter manually:
<!-- ① Manage Spring AI module versions -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>2.0.0-M8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Web interface -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- DeepSeek model starter – auto‑configures ChatClient -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-deepseek</artifactId>
</dependency>
</dependencies>Because the milestone version is not in Maven Central, add the Spring Milestones repository:
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>Configuration
Add the following properties to src/main/resources/application.properties:
# DeepSeek API key (inject via environment variable)
spring.ai.deepseek.api-key=${DEEPSEEK_API_KEY}
# Model name – deepseek-chat (general) or deepseek-reasoner (reasoning)
spring.ai.deepseek.chat.model=deepseek-chat
# Sampling temperature, 0 = most stable, higher = more creative
spring.ai.deepseek.chat.temperature=0.7Set the environment variable DEEPSEEK_API_KEY before starting; the starter already provides the base URL.
First controller
Create a @RestController that injects the auto‑configured ChatClient.Builder, builds a ChatClient, and exposes /ai/chat:
@RestController
public class ChatController {
private final ChatClient chatClient;
// Builder is auto‑configured by the DeepSeek starter
public ChatController(ChatClient.Builder builder) {
this.chatClient = builder.build();
}
@GetMapping("/ai/chat")
public String chat(@RequestParam(defaultValue = "Introduce yourself in one sentence") String message) {
return chatClient
.prompt(message) // send user question
.call() // synchronous model call
.content(); // extract text reply
}
}Run and test
export DEEPSEEK_API_KEY=sk‑yourkey # inject env var
mvn spring-boot:run # start the appAfter the application starts, invoke the endpoint:
curl "http://localhost:8080/ai/chat?message=你好"
# Expected output: 你好!我是 DeepSeek,很高兴为你服务……The full call chain is:
Controller → chatClient.prompt(msg).call().content()
↓
ChatClient (fluent façade)
↓
DeepSeekChatModel (auto‑wired by starter) → HTTP request → DeepSeek API → responseOnly the controller code is written by the developer; the rest (ChatClient, model wiring, HTTP handling, JSON parsing) is supplied by the starter, illustrating Spring AI’s “convention over configuration”.
Key concepts
Spring AI – Spring‑style AI application framework using ChatClient to call large models.
Modularization (2.0) – independent modules; add only the ones you need.
spring-ai-bom – manages versions of all Spring AI modules.
spring-ai-starter-model-deepseek – DeepSeek model starter that auto‑configures ChatClient.
ChatClient – unified façade for model calls with a fluent API.
prompt().call().content() – three‑step flow: ask → invoke → get reply.
Reference links
Spring AI documentation: https://docs.spring.io/spring-ai/reference/
DeepSeek integration guide: https://docs.spring.io/spring-ai/reference/api/chat/deepseek-chat.html
DeepSeek platform: https://platform.deepseek.com/
Spring Initializr: https://start.spring.io/
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Tech Ocean
Focused on AI programming, sharing ready-to-use development efficiency solutions.
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.
