跪拜 Guibai
← All articles
Spring

Spring AI 2.0 Gives Java a Native On-Ramp to LLM Chat with Redis-Backed Memory

By 皮皮林551 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Java teams that want LLM features no longer need to bolt on a separate service or hand-roll HTTP calls. Spring AI's abstraction and memory support cut integration time to a few configuration lines, and the OpenAI-compatible base-url means existing DeepSeek or OpenAI keys work as-is, making multi-vendor model swapping trivial.

Summary

Spring AI 2.0 extends the Spring ecosystem into AI, letting Java developers wire LLM chat into a project the same way they wire a database or a web controller. A single ChatClient bean wraps all the HTTP and streaming details; adding one starter dependency and a configuration class gets sync call() and stream() endpoints running. Because the OpenAI-compatible base-url accepts any compatible vendor, DeepSeek's endpoint works with the same code that targets OpenAI.

The conversation memory story is the practical centerpiece. Without it, every prompt looks like a first conversation. Spring AI's ChatMemory interface and its Redis repository store message history per conversationId, and a few lines in the controller fetch prior context, add new user and assistant messages, and clear history. The walkthrough covers launching redis-stack via Docker, creating RedisChatMemoryRepository and MessageWindowChatMemory beans, and wiring the controller's call and stream methods to persist state.

The example runs in a standard Spring Boot app with WebFlux, and includes a TDesign chatbot UI for testing. The takeaway is that Spring AI is not a toy wrapper; it offers a cohesive way to add LLM features while staying inside the framework Java developers already know, with no separate Python service or hand-rolled HTTP client.

Takeaways
— Spring AI 2.0 provides a ChatClient bean with synchronous call() and streaming stream() methods, mirroring the WebClient style.
— Any OpenAI-compatible API, such as DeepSeek, can be configured via base-url in application.yaml without changing code.
— Redis-backed conversation memory requires redis-stack (version 7.4.0 in the example), not plain Redis.
— ChatMemory must be manually tied to a conversationId; the controller fetches history, adds user and assistant messages, and clears history through its add, get, and clear methods.
— The stream() endpoint returns SSE events and appends a STOP event after the final message.
— The sample project includes a TDesign chatbot UI to test both sync and streaming chat.
— The full source is available at https://github.com/macrozheng/spring-ai-examples.
Conclusions

Modeling the client after Spring's WebClient style lowers the learning curve for Java developers already comfortable with reactive streams, making AI features feel like a natural extension of the framework.

Even with Spring AI, conversation state remains the developer's responsibility; the controller manually calls add and get, so the framework provides storage but not magic persistence.

Using an OpenAI-compatible base-url for DeepSeek shows how easy model vendor swapping becomes, but it also means vendor-specific features may be hidden behind the compatibility layer.

Concepts & terms
ChatClient
A Spring AI abstraction that wraps all interactions with an LLM, offering methods like prompt(), user(), call(), and stream() so developers never touch raw HTTP or streaming internals.
ChatMemory
An interface for storing and retrieving multi-turn conversation history. Implementations back it with in-memory stores, Redis, JDBC, and more, and it is keyed by conversationId.
SSE (Server-Sent Events)
A one-way HTTP streaming protocol. Spring AI's stream() method returns SSE events, letting a client receive model output token by token instead of waiting for the full response.
Tool Calling
A feature where the LLM can invoke registered Java methods via the @Tool annotation, enabling the model to execute real operations such as database queries or API calls.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗