Spring AI 2.0: Build an AI Chat Service with Redis Memory in Three Steps
This tutorial demonstrates how to integrate Spring AI 2.0 into a Spring Boot project, configure OpenAI-compatible models like DeepSeek, implement synchronous and streaming chat endpoints, and add multi-turn conversation memory using Redis-backed ChatMemory, all with minimal code.
Spring AI Introduction
Spring AI is the official Spring ecosystem extension for artificial intelligence, enabling Java developers to build AI applications as naturally as using Spring MVC or Spring Data. Its core features include:
Multi-model, multi-provider support : One codebase adapts to OpenAI, DeepSeek, Anthropic, Ollama, covering chat, text-to-image, speech-to-text.
ChatClient streaming API : A WebClient-style conversation client supporting synchronous call() and streaming stream() invocations.
Advisors API : Encapsulates RAG, conversation memory, safety filters as reusable interceptor chains portable across models and scenarios.
Conversation Memory (Chat Memory) : Built-in in-memory, Redis, JDBC storage backends that automatically manage multi-turn dialogue history.
RAG Retrieval-Augmented Generation : Built-in ETL pipeline splits, vectorizes, and writes documents to a vector store; one line of code lets the model "read" your documents.
Tool Calling : Register Java methods with @Tool annotation so the model can invoke your APIs to perform real actions.
Project Integration with Spring AI
Add Dependencies
First, add Spring AI dependency management in pom.xml to unify versions (using 2.0.0):
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>Because many models are OpenAI-API compatible, add the OpenAI model starter and WebFlux for SSE streaming:
<!-- Spring AI OpenAI Starter -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
<!-- SpringBoot WebFlux for Flux/Mono reactive types -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>Add Configuration
In application.yaml, configure the model connection (using DeepSeek via OpenAI-compatible endpoint):
spring:
ai:
openai:
# Access DeepSeek service via OpenAI-compatible interface
base-url: https://api.deepseek.com
# Corresponding API_KEY
api-key: ${DEEPSEEK_API_KEY}
chat:
# Model ID, supports deepseek-v4-flash, deepseek-v4-pro
model: deepseek-v4-proCreate a Java configuration class with a ChatClient bean; ChatClient encapsulates all communication details with the AI model:
@Configuration
public class SpringAIConfig {
@Bean
public ChatClient chatClient(ChatClient.Builder builder) {
return builder.build();
}
}Implement Chat Functionality
Create ChatController with synchronous call and streaming stream endpoints. The streaming endpoint produces TEXT_EVENT_STREAM_VALUE.
@RequiredArgsConstructor
@RestController
@RequestMapping("/chat")
@Tag(name = "ChatController", description = "Chat-related APIs")
public class ChatController {
private final ChatClient chatClient;
@Operation(summary = "Synchronous call to LLM")
@PostMapping("/call")
public String call(@RequestParam String question, @RequestParam String conversationId) {
return this.chatClient.prompt()
.user(question)
.call()
.content();
}
@Operation(summary = "Streaming call to LLM")
@PostMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ChatEventDto> stream(@RequestParam String question, @RequestParam String conversationId) {
return this.chatClient.prompt()
.user(question)
.stream()
.content()
.map(content -> ChatEventDto.builder()
.eventType(ChatEventType.DATA.getValue())
.eventData(content)
.build())
.concatWith(Flux.just(ChatEventDto.builder()
.eventType(ChatEventType.STOP.getValue())
.build()));
}
}ChatEventDtowraps SSE events as JSON:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(title = "ChatEventDto", description = "SSE session event result")
public class ChatEventDto {
@Schema(description = "Event type: 1001-data, 1002-stop, 1003-params")
private Integer eventType;
@Schema(description = "Message content")
private Object eventData;
}Testing with Postman shows synchronous /call returns the full response at once (slower), while /stream returns JSON chunks progressively. A follow-up question "What did I just ask? Which turn is this?" reveals the model treats it as the first conversation — no memory yet.
Implement Conversation Memory
Spring AI supports multiple memory stores (in-memory, JDBC, Neo4j, MongoDB, Redis). This guide uses Redis (requires Redis Stack, not plain Redis). Run Redis Stack 7.4.0 via Docker:
docker run --name redis-stack \
-p 6379:6379 \
-p 8001:8001 \
-d redis/redis-stack:7.4.0-v8Add the Redis Chat Memory starter:
<!-- Spring AI Redis Chat Memory Repository -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-chat-memory-repository-redis</artifactId>
</dependency>Configure Redis connection in application.yaml:
spring:
ai:
chat:
memory:
redis:
host: 192.168.3.101
port: 6379
time-to-live: 24h
key-prefix: spring-chatIn the configuration class, create RedisChatMemoryRepository and ChatMemory beans. MessageWindowChatMemory retains the last 100 messages:
@Configuration
public class SpringAIConfig {
@Value("${spring.ai.chat.memory.redis.host}")
private String redisHost;
@Value("${spring.ai.chat.memory.redis.port}")
private int redisPort;
@Bean
public RedisChatMemoryRepository redisChatMemoryRepository() {
RedisClient redisClient = RedisClient.builder()
.hostAndPort(redisHost, redisPort)
.build();
return RedisChatMemoryRepository.builder()
.jedisClient(redisClient)
.build();
}
@Bean
public ChatMemory chatMemory(RedisChatMemoryRepository repository) {
return MessageWindowChatMemory.builder()
.chatMemoryRepository(repository)
.maxMessages(100)
.build();
}
}Update ChatController to inject ChatMemory, manually add user messages, retrieve history for context, and save assistant replies. For streaming, accumulate chunks in a StringBuilder and persist on completion. Also add /history and /clearHistory endpoints.
@RequiredArgsConstructor
@RestController
@RequestMapping("/chat")
@Tag(name = "ChatController", description = "Chat-related APIs")
public class ChatController {
private final ChatClient chatClient;
private final ChatMemory chatMemory;
@Operation(summary = "Synchronous call to LLM")
@PostMapping("/call")
public String call(@RequestParam String question, @RequestParam String conversationId) {
List<Message> historyMessages = chatMemory.get(conversationId);
chatMemory.add(conversationId, List.of(new UserMessage(question)));
String response = this.chatClient.prompt()
.messages(historyMessages)
.user(question)
.call()
.content();
chatMemory.add(conversationId, List.of(new AssistantMessage(response)));
return response;
}
@Operation(summary = "Streaming call to LLM")
@PostMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ChatEventDto> stream(@RequestParam String question, @RequestParam String conversationId) {
List<Message> historyMessages = chatMemory.get(conversationId);
chatMemory.add(conversationId, List.of(new UserMessage(question)));
StringBuilder fullResponse = new StringBuilder();
return this.chatClient.prompt()
.messages(historyMessages)
.user(question)
.stream()
.content()
.map(content -> {
fullResponse.append(content);
return ChatEventDto.builder()
.eventType(ChatEventType.DATA.getValue())
.eventData(content)
.build();
})
.concatWith(Flux.just(ChatEventDto.builder()
.eventType(ChatEventType.STOP.getValue())
.build()))
.doOnComplete(() -> {
if (!fullResponse.isEmpty()) {
chatMemory.add(conversationId, List.of(new AssistantMessage(fullResponse.toString())));
}
});
}
@Operation(summary = "Get conversation history")
@GetMapping("/history")
public CommonResult<List<Map<String, Object>>> history(@RequestParam String conversationId) {
var result = chatMemory.get(conversationId).stream()
.map(msg -> Map.<String, Object>of(
"role", msg.getMessageType().name(),
"content", msg.getText()))
.collect(Collectors.toList());
return CommonResult.success(result);
}
@Operation(summary = "Clear conversation history")
@PostMapping("/clearHistory")
public CommonResult<Void> clearHistory(@RequestParam String conversationId) {
chatMemory.clear(conversationId);
return CommonResult.success(null);
}
}Functional Testing
Using a TDesign Chatbot UI, test conversation memory: ask a question, then ask "What did I ask before? Which turn?" The model answers correctly, proving memory works. Redis console shows stored dialogue data. Refreshing the page loads history via /history.
Summary
This article walked through building an AI chat service with conversation memory using Spring AI 2.0 from scratch. Core steps: add starter dependencies, configure model parameters, inject ChatClient — a few lines enable synchronous chat and SSE streaming. Adding ChatMemory backed by Redis solves the "every chat is the first chat" problem. Spring AI lets you integrate AI capabilities into your project in the most familiar Spring way with minimal code.
Project Source Code
https://github.com/macrozheng/spring-ai-examples
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.
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.
