)
Spring AI: the most "Java" way to do GenAI in business
Spring, Java, and Generative AI?
Since the birth of generative AI, Python has been the reference language, thanks to specific tools and libraries.
However, with the arrival of Spring AI, the official version released in May 2025, it is finally possible to leverage GenAI even in enterprise Java environments, adhering to the foundational principles of Spring while benefiting from the reliability and scalability typical of the platform.
Why adopt Spring AI?
Just add the starter, set your API key, and you can immediately use the various components as if they were native Spring elements
By coding through Spring AI interfaces, you can switch between providers (OpenAI, Vertex AI, or others) without changing any logic
Structured outputs can be directly mapped to Java POJOs, making the interaction type-safe and reliable
Using MCP and @Tool, you can create assistants capable of performing real-world actions, enriching the interaction with external tools
Real use case: Spring enhanced with OpenAI
The underlying idea was to transform a classic Spring application for managing trophies won by players into something more natural: a virtual assistant that understands questions in everyday language.
Instead of navigating through tabs and forms, an operator can simply write:
"List the information about players named Daniela",
and receive an immediate and precise response from the AI.
What happens?
Spring AI receives the request and, before sending it to OpenAI, enriches it with a set of data, for example: a preconfigured prompt that explains the context, any previous messages, and the list of available APIs.
Thanks to the structure of the prompt and the description of the APIs, OpenAI autonomously understands that it needs to invoke a specific function, in this case listPlayers.
Spring AI converts the request and executes the query via Spring Data JPA.
A PlayersResponse is returned to OpenAI with the list of players in JSON format.
OpenAI processes it and generates the final message in natural language, for example:
“The player named Daniela in our records is: Daniela Rossi – Address: Via Roma 123 – Tel: 0612345678 – List of trophies won: PlayerOfTheMonth, Top1%Weekly, PlayerElite.”
All steps are logged in the application logs, highlighting how Spring AI manages tool calls, context, session IDs, and more.
Why is it an ideal reference case?
This example clearly showcases the power of Spring AI, enabling a natural and fluid interaction with the system without the need for complex user interfaces.
Existing Java business logic can be reused without requiring invasive refactoring, ensuring development continuity and saving time.
In addition, by integrating RAG capabilities based on embeddings, it becomes possible to run semantic queries even on small datasets, improving AI response quality.
The system also stands out for its flexibility: you can switch from a provider like OpenAI to others, such as Azure AI, without having to modify your code.
Finally, thanks to the use of a preloaded vector store and a lean architecture, application startup is fast and operational costs are kept low.