Spring AI 2.0 Gives Java a Native On-Ramp to LLM Chat with Redis-Backed Memory
Why Are More and More People Using Spring AI?
Author: 皮皮林551
Introduction to Spring AI
Spring AI is the official extension of the Spring ecosystem for the AI field. It aims to let Java developers build AI applications as naturally as they do with Spring MVC or Spring Data, without needing to dive into the underlying machine learning internals.
The core features of Spring AI are as follows:
- Multi-model, multi-vendor support: One set of code adapts to mainstream models like OpenAI, Deepseek, Anthropic, and Ollama, covering chat, text-to-image, speech-to-text, and more.
- ChatClient streaming API: A chat client consistent with the WebClient style, supporting both synchronous
call()and streamingstream()methods. - Advisors API: Encapsulates AI patterns such as RAG, conversation memory, and safety filtering into a reusable interceptor chain that can be ported across models and scenarios.
- Chat Memory: Built-in memory, Redis, JDBC, and other storage backends to automatically manage multi-turn conversation history context.
- RAG (Retrieval-Augmented Generation): Built-in ETL pipeline that chunks and vectorizes documents into a vector database. One line of code lets the large model "read" your documents.
- Tool Calling: Register Java methods with the
@Toolannotation so the model can call your APIs to perform real operations.
Integrating Spring AI into a Project
Step 1: Integrate Spring AI into your project.
Add Dependencies
- First, add Spring AI's dependency management to
pom.xmlto centrally manage Spring AI versions. Here, version 2.0.0 is used.
<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>
- Since many models are compatible with the OpenAI API, we use the OpenAI model Starter here. We also need to implement SSE streaming, so we add the WebFlux Starter as well.
<!-- Spring AI OpenAI Starter -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
<!-- SpringBoot WebFlux dependency module (provides Flux/Mono reactive types) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
Add Configuration
- Next, add the model invocation configuration in
application.yaml, including base-url, api-key, and chat.model. Here, thedeepseek-v4-promodel is used.
spring:
ai:
openai:
# Access DeepSeek service through OpenAI-compatible API
base-url: https://api.deepseek.com
# Corresponding API_KEY
api-key: ${DEEPSEEK_API_KEY}
chat:
# Model ID to use, supports deepseek-v4-flash and deepseek-v4-pro
model: deepseek-v4-pro
- Finally, add a Java configuration class to configure a ChatClient bean. The ChatClient class encapsulates all the details of communicating with the AI model. You only need to call its methods, without worrying about the underlying HTTP requests, streaming, and so on.
/**
* Spring AI configuration class
* @author macrozheng
* @since 2026/6/29
*/
@Configuration
public class SpringAIConfig {
@Bean
public ChatClient chatClient(ChatClient.Builder builder) {
return builder.build();
}
}
Implementing Chat Functionality
Spring AI integration is complete; now let's develop a chat feature to test it.
- Create a
ChatControllerthat implements the synchronouscallmethod and the streamingstreammethod of the large model. For the streaming method, set the output format toTEXT_EVENT_STREAM_VALUE.
/**
* Chat function related endpoints
* @author macrozheng
* @since 2026/6/29
*/
@RequiredArgsConstructor
@RestController
@RequestMapping("/chat")
@Tag(name = "ChatController", description = "Chat function related endpoints")
public class ChatController {
private final ChatClient chatClient;
@Operation(summary = "Synchronous call to large model")
@PostMapping("/call")
public String call(@RequestParam String question, @RequestParam String conversationId) {
return this.chatClient.prompt()
.user(question)
.call()
.content();
}
@Operation(summary = "Streaming call to large model")
@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()
// For each message in the stream, convert to ChatEventDto object
.map(content -> ChatEventDto.builder()
.eventType(ChatEventType.DATA.getValue())
.eventData(content)
.build())
// Append a STOP message after the stream ends
.concatWith(Flux.just(ChatEventDto.builder()
.eventType(ChatEventType.STOP.getValue())
.build()));
}
}
ChatEventDtoencapsulates the SSE session event return result, making the SSE chat return JSON-formatted data.
/**
* SSE session event return result
* @author macrozheng
* @since 2026/7/1
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(title = "ChatEventDto", description = "SSE session event return result")
public class ChatEventDto {
@Schema(description = "Event type: 1001 - data event, 1002 - stop event, 1003 - parameter event")
private Integer eventType;
@Schema(description = "Message content")
private Object eventData;
}
- Here we test the synchronous
callinterface using Postman. It responds slowly and returns all information at once.
- Test the asynchronous
streaminterface next; it returns JSON-formatted information gradually.
- Following the conversation above, ask: "What question did I just ask? How many conversations has this been?" The large model replies that this is the first conversation, showing that conversation memory is not yet implemented.
Implementing Conversation Memory
So how do we implement conversation memory? Spring AI supports multiple types of memory storage to suit different use cases, such as in-memory, JDBC, Neo4j, MongoDB, and Redis. Here, we use Redis as an example to show how to implement conversation memory.
- Redis conversation memory requires
redis-stack, not the regular Redis version. Here, we use redis-stack version 7.4.0. The Docker command is as follows:
docker run --name redis-stack \
-p 6379:6379 \
-p 8001:8001 \
-d redis/redis-stack:7.4.0-v8
- Then add the Redis conversation memory Starter to the project's
pom.xml:
<!-- Spring AI Redis Chat Memory Repository -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-chat-memory-repository-redis</artifactId>
</dependency>
- Add Redis connection configuration in
application.yaml:
spring:
ai:
chat:
memory:
redis:
host: 192.168.3.101
port: 6379
time-to-live: 24h
key-prefix: spring-chat
- Create the
RedisChatMemoryRepositoryandChatMemorybeans in the Java configuration class. ThroughChatMemory, we can make the AI model "remember" historical conversation content in the same session, enabling true multi-turn dialogue.
/**
* Spring AI configuration class
* @author macrozheng
* @since 2026/6/29
*/
@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();
}
}
- Next, inject
ChatMemoryintoChatController. Use itsaddmethod to save conversation memory,getto retrieve memory, andclearto clear it. All memory must be bound to the correspondingconversationId.
/**
* Chat function related endpoints
* @author macrozheng
* @since 2026/6/29
*/
@RequiredArgsConstructor
@RestController
@RequestMapping("/chat")
@Tag(name = "ChatController", description = "Chat function related endpoints")
public class ChatController {
private final ChatClient chatClient;
private final ChatMemory chatMemory;
@Operation(summary = "Synchronous call to large model")
@PostMapping("/call")
public String call(@RequestParam String question, @RequestParam String conversationId) {
// Get historical messages as context
List<Message> historyMessages = chatMemory.get(conversationId);
// Manually save user message
chatMemory.add(conversationId, List.of(new UserMessage(question)));
// Call the large model
String response = this.chatClient.prompt()
.messages(historyMessages)
.user(question)
.call()
.content();
// Manually save assistant response
chatMemory.add(conversationId, List.of(new AssistantMessage(response)));
return response;
}
@Operation(summary = "Streaming call to large model")
@PostMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ChatEventDto> stream(@RequestParam String question, @RequestParam String conversationId) {
// Get historical messages as context
List<Message> historyMessages = chatMemory.get(conversationId);
// Manually save user message
chatMemory.add(conversationId, List.of(new UserMessage(question)));
// Used to collect the full assistant response
StringBuilder fullResponse = new StringBuilder();
return this.chatClient.prompt()
.messages(historyMessages)
.user(question)
.stream()
.content()
// For each message in the stream, convert to ChatEventDto object
.map(content -> {
fullResponse.append(content);
return ChatEventDto.builder()
.eventType(ChatEventType.DATA.getValue())
.eventData(content)
.build();
})
// Append a STOP message after the stream ends
.concatWith(Flux.just(ChatEventDto.builder()
.eventType(ChatEventType.STOP.getValue())
.build()))
.doOnComplete(() -> {
// Save assistant response after stream completes
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);
}
}
Feature Testing
I built a chat interface using the TDesign Chatbot component. Let's see the results together.
- First, test the conversation memory feature. For example, ask a question, then ask "What question did I ask before? Which conversation number is this?" The model answers correctly, verifying that conversation memory is implemented.
- Open the Redis management console; you can see the stored conversation memory data.
- We also implemented the
get conversation historyendpoint in the controller. Refresh the page, and it retrieves the history based on that endpoint.
Summary
This article takes you from scratch to build an AI chat service with conversation memory using Spring AI 2.0. The core steps are simple: add the Starter dependency, configure model parameters, and inject ChatClient to call. A few lines of code achieve both synchronous chat and SSE streaming output.
On top of that, by introducing ChatMemory and connecting to Redis, multi-turn conversation memory is easily implemented, completely solving the problem that "every time is the first conversation" for large models.
Spring AI lets you integrate AI capabilities into your project with the most familiar Spring style and the least amount of code. If you're interested, give it a try!