跪拜 Guibai
← Back to the summary

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:

Integrating Spring AI into a Project

Step 1: Integrate Spring AI into your project.

Add Dependencies

<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>
<!-- 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

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
/**
 * 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.

/**
 * 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()));
    }
}
/**
 * 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;
}

image

image

image

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.

docker run --name redis-stack \
-p 6379:6379 \
-p 8001:8001 \
-d redis/redis-stack:7.4.0-v8
<!-- Spring AI Redis Chat Memory Repository -->
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-chat-memory-repository-redis</artifactId>
</dependency>
spring:
  ai:
    chat:
      memory:
        redis:
          host: 192.168.3.101
          port: 6379
          time-to-live: 24h
          key-prefix: spring-chat
/**
 * 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();
    }
}
/**
 * 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.

image

image

image

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!

Project Source Code

https://github.com/macrozheng/spring-ai-examples