AI is showing up everywhere, from support chatbots and content generation to searching internal documents. For Spring Boot developers, adding these features used to mean hand-writing HTTP calls, managing prompts, parsing JSON responses and juggling embeddings. Spring AI removes most of that work. It lets you talk to AI models through the same kind of Spring-style APIs you already use every day.
In this guide, we'll cover what Spring AI is, where it's useful, and how to build a small AI-powered app with Spring Boot in five steps.
What is Spring AI?
Spring AI is a project in the Spring ecosystem that gives Java developers a common API for working with AI models. Its 2.0 release supports providers including OpenAI, Anthropic, Google GenAI, Amazon Bedrock, Mistral AI, DeepSeek and Ollama (Spring Blog, 2026), so your code doesn't have to be tied to a single vendor.
In simple terms:
Spring Boot Application
↓
Spring AI
↓
AI Model
↓
ResponseInstead of writing provider-specific API calls by hand, you work with common interfaces. The main one is ChatClient, which gives you a fluent way to send prompts and read responses.
Why use Spring AI instead of calling the API directly?
Spring Boot is used by 14.7% of all developers, and Java by 29.4% (Stack Overflow Developer Survey, 2025). For that audience, Spring AI means adding AI without leaving familiar patterns like dependency injection, auto-configuration and application.properties.
Say you want to add a chatbot to your app. Without Spring AI, you'd normally handle:
- Creating HTTP requests
- Adding API authentication
- Building the request JSON
- Calling the AI API
- Parsing the response
Spring AI handles that boilerplate. And because it uses common abstractions, swapping one model provider for another is mostly a configuration change. Isn't that how the rest of your Spring stack already works?
Where is Spring AI useful?
Spring AI fits wherever a Spring Boot app needs to understand or generate language. With 84% of developers already using or planning to use AI tools (Stack Overflow, 2025), the common starting points look like this:
- AI chatbots and virtual assistants
- Question-answer systems
- Document search and summarization
- Content generation
- Semantic search and RAG (retrieval-augmented generation)
- AI-powered business actions
For example, a support app can use Spring AI to answer questions from company documents instead of relying only on canned responses.
What can you do with Spring AI?
Spring AI covers five capabilities most beginners need: chat, prompt templates, embeddings, vector stores and tool calling. The 2.0 release also builds in Model Context Protocol (MCP) support (Spring Blog, 2026), but you won't need that for your first app.
1. Chat and text generation
Send a prompt to an AI model and get a response back.
Write a welcome message for a new customer.2. Prompt handling
Build dynamic prompts from user input or application data.
Explain {topic} in simple English.3. Embeddings
Convert text into vectors so you can find similar or related content. Embeddings power semantic search, document search and RAG.
"I forgot my password."
↓
[0.12, -0.43, 0.87, ...]4. Vector stores
Store and search embeddings in a vector database such as PostgreSQL with PGVector. This is the backbone of most RAG applications.
5. Tool calling
Let the AI model call methods in your application to fetch real-time data. For example, the model can call your getOrderStatus() method to answer:
What is the status of order 12345?How does Spring AI work?
A Spring AI request follows the same path as any other Spring Boot request, with ChatClient sitting between your service layer and the model:
User
↓
Spring Boot Controller
↓
ChatClient
↓
AI Model
↓
AI Response
↓
UserA user sends something like "Explain Spring Boot in simple words." Your app passes that prompt through Spring AI to the configured model, and the generated answer goes back to the user.
How do you get started with Spring AI?
You need a Spring Boot 4.0.x or 4.1.x project, since Spring AI 2.0.x supports only those versions (Spring AI Docs, 2026). Projects still on Boot 3.x will need to upgrade first. We'll build an app that takes a question and returns an AI-generated answer, using OpenAI as the provider.
Step 1: Create a Spring Boot project
Create a project with Spring Initializr and add:
- Spring Web
- OpenAI (under the AI section)
Initializr lets you pick whichever model integration you need, and it adds the Spring AI BOM for you.
Step 2: Add the Spring AI dependencies
If you're adding Spring AI to an existing Maven project, you need two things: the BOM, which manages versions, and the OpenAI starter.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>2.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
</dependencies>Don't skip the BOM. The starter has no version of its own, so without the BOM your build will fail.
Step 3: Configure OpenAI
Add this to application.properties:
spring.ai.openai.api-key=${OPENAI_API_KEY}
spring.ai.openai.chat.model=gpt-5-minigpt-5-mini is Spring AI's default OpenAI chat model, so the second line is optional. Keep it if you want the choice to be explicit.
Keep your API key in an environment variable, never in source code:
export OPENAI_API_KEY=your-api-keyStep 4: Create the AI service
Now create a service that uses ChatClient. Spring Boot auto-configures a ChatClient.Builder for you.
@Service
public class AiService {
private final ChatClient chatClient;
public AiService(ChatClient.Builder chatClientBuilder) {
this.chatClient = chatClientBuilder.build();
}
public String ask(String question) {
return chatClient
.prompt()
.user(question)
.call()
.content();
}
}Only four calls matter here:
- prompt()
- user(question)
- call()
- content()
Step 5: Create the controller
Expose the service through a REST endpoint.
@RestController
@RequestMapping("/api/ai")
public class AiController {
private final AiService aiService;
public AiController(AiService aiService) {
this.aiService = aiService;
}
@GetMapping("/ask")
public String ask(@RequestParam String question) {
return aiService.ask(question);
}
}Run the app and call:
GET /api/ai/ask?question=What is Spring Boot?You'll get a response along these lines:
Spring Boot is a framework built on top of Spring
that makes it easier to create Java applications
with less configuration.That's it. Your Spring Boot app is now talking to an AI model through Spring AI.
What are embeddings in Spring AI?
An embedding turns text into a list of numbers (a vector) that represents its meaning. Spring AI exposes this through the EmbeddingModel API, and texts with similar meanings end up with similar vectors. That's what makes semantic search and RAG possible.
"I forgot my password."
↓
Embedding Model
↓
[0.12, -0.31, 0.74, ...]With the OpenAI starter on the classpath, you can inject EmbeddingModel and call it directly:
float[] vector = embeddingModel.embed("I forgot my password.");You'd then store these vectors in a vector database and search them to find content related to a user's question. A query like "can't log in" would match the password-reset doc even though the two share no keywords.
Final thoughts
Spring AI makes it easier for Spring Boot developers to start working with AI. You don't need to learn every feature before shipping your first AI endpoint.
The core idea is simple. Spring AI gives Java developers a familiar, Spring-style way to add AI to their applications. Start with ChatClient, get one endpoint working, then add embeddings, vector stores or tool calling as your app needs them.
