Spring AI Introduction - Build AI Applications with Java & Spring Boot
Learn Spring AI from scratch. Understand its architecture, core concepts, supported AI models, and build your first AI-powered Spring Boot application step by step.
Spring AI is an official Spring ecosystem project that makes it easy for Java developers to build AI-powered applications using familiar Spring programming models.
Instead of learning different SDKs for every AI provider, Spring AI provides a consistent abstraction that works across multiple AI platforms.
Think of it like Spring Data JPA.
Instead of writing database-specific code for MySQL, PostgreSQL, Oracle, or SQL Server, Spring Data provides one common API.
Similarly,
Instead of writing different code for OpenAI, Azure OpenAI, Anthropic Claude, Google Gemini, Amazon Bedrock, or Ollama, Spring AI provides one unified API.
Why Spring AI?
Before Spring AI, developers had to:
- Learn each AI provider SDK
- Handle REST API calls manually
- Manage authentication
- Parse JSON responses
- Handle retries
- Handle token limits
- Build prompt templates manually
Spring AI simplifies all of this.
Traditional AI Integration
flowchart LR
A[Spring Boot Application]
B[OpenAI SDK]
C[Gemini SDK]
D[Claude SDK]
E[Bedrock SDK]
F[REST APIs]
A --> B
A --> C
A --> D
A --> E
B --> F
C --> F
D --> F
E --> F
Problems:
- Vendor lock-in
- Different APIs
- Different request formats
- More maintenance
Spring AI Approach
flowchart LR
App[Spring Boot Application]
AI[Spring AI]
OpenAI[OpenAI]
Gemini[Google Gemini]
Claude[Anthropic Claude]
Bedrock[Amazon Bedrock]
Ollama[Ollama Local]
App --> AI
AI --> OpenAI
AI --> Gemini
AI --> Claude
AI --> Bedrock
AI --> Ollama
One API.
Multiple AI providers.
Where Can Spring AI Be Used?
Spring AI can build:
- ChatGPT-like applications
- AI chatbots
- Customer support bots
- Knowledge assistants
- Document Q&A
- PDF search
- Resume analyzers
- Email generators
- Code generators
- SQL generators
- AI agents
- Multi-agent systems
- Recommendation engines
- Enterprise copilots
Real Enterprise Example
Imagine a banking application.
Customer asks:
"Why was my credit card payment declined?"
Without AI:
Customer support manually checks multiple systems.
With Spring AI:
Customer
│
▼
Spring Boot
│
▼
Spring AI
│
▼
LLM
│
▼
Knowledge Base
│
▼
Answer
The AI can search documentation, transaction history (with proper authorization), and FAQs to generate a human-readable response.
Spring AI Architecture
flowchart TD
User
Controller
Prompt
ChatClient
AIModel
OpenAI
Response
User --> Controller
Controller --> Prompt
Prompt --> ChatClient
ChatClient --> AIModel
AIModel --> OpenAI
OpenAI --> Response
Spring AI Modules
Spring AI consists of multiple modules.
Chat Models
Used for conversations.
Examples:
- ChatGPT
- Claude
- Gemini
- Llama
Embedding Models
Convert text into vectors.
Used for:
- Semantic search
- Similarity search
- Recommendation systems
- RAG
Image Models
Generate images from prompts.
Example:
Generate a futuristic city skyline.
Audio Models
Speech-to-text
Text-to-speech
Voice assistants
Moderation Models
Detect:
- Toxic content
- Hate speech
- Violence
- Unsafe content
Vector Stores
Store embeddings.
Supported stores include:
- PostgreSQL + PGVector
- ChromaDB
- Milvus
- Redis
- Elasticsearch
- Pinecone
- Weaviate
- Azure AI Search
Core Components
Prompt
A prompt is the instruction sent to the AI.
Example:
Explain Java Streams with examples.
ChatClient
The main API developers use.
String response =
chatClient.prompt("Hello")
.call()
.content();
ChatModel
Responsible for communicating with the LLM.
Application
↓
ChatClient
↓
ChatModel
↓
AI Provider
EmbeddingModel
Converts text into vectors.
Text
↓
Embedding Model
↓
Vector
↓
Database
Vector Store
Stores vectors.
Used for semantic search.
Supported AI Providers
Spring AI currently supports:
- OpenAI
- Azure OpenAI
- Google Gemini
- Anthropic Claude
- Ollama
- Amazon Bedrock
- Mistral AI
- Hugging Face
- Cohere
- DeepSeek (via compatible APIs)
This abstraction allows changing providers with minimal application code changes.
Spring AI Project Structure
spring-ai-demo
src
├── controller
│ ChatController
│
├── service
│ AIService
│
├── config
│ AIConfig
│
└── SpringAiApplication
Create Spring Boot Project
Dependencies:
- Spring Web
- Spring AI
- OpenAI Starter (or another provider)
- Lombok (optional)
Maven Dependency
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
application.yml
spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
chat:
options:
model: gpt-4.1-mini
Using environment variables keeps secrets out of source control.
Simple AI Service
@Service
public class AIService {
private final ChatClient chatClient;
public AIService(ChatClient.Builder builder) {
this.chatClient = builder.build();
}
public String ask(String question) {
return chatClient
.prompt(question)
.call()
.content();
}
}
REST Controller
@RestController
@RequestMapping("/chat")
public class ChatController {
private final AIService aiService;
public ChatController(AIService aiService) {
this.aiService = aiService;
}
@GetMapping
public String chat(@RequestParam String prompt) {
return aiService.ask(prompt);
}
}
Test the API
Request
GET /chat?prompt=Explain Spring Boot
Response
Spring Boot is a Java framework that simplifies the development of production-ready Spring applications by providing auto-configuration, embedded servers, starter dependencies, and production features.
Request Flow
sequenceDiagram
User->>Controller: GET /chat
Controller->>AIService: ask(prompt)
AIService->>ChatClient: prompt()
ChatClient->>OpenAI: API Request
OpenAI-->>ChatClient: AI Response
ChatClient-->>AIService: Response
AIService-->>Controller: String
Controller-->>User: JSON/Text
Why Java Developers Love Spring AI
- Familiar Spring Boot programming model
- Dependency Injection
- Auto Configuration
- Easy provider switching
- Enterprise-ready architecture
- Integration with Spring Security
- Spring Data compatibility
- Production monitoring with Micrometer
- Easy testing and mocking
- Consistent API across AI providers
Best Practices
✅ Keep API keys in environment variables.
✅ Use prompt templates instead of hardcoding prompts.
✅ Validate user input before sending it to AI.
✅ Log requests responsibly without exposing sensitive data.
✅ Monitor token usage and latency.
✅ Implement retries and timeouts.
✅ Cache responses where appropriate.
Common Use Cases
| Use Case | Example |
|---|---|
| Chatbot | Customer support |
| RAG | Company documentation search |
| Code Generation | Generate Java code |
| SQL Generator | Convert English to SQL |
| Email Assistant | Draft business emails |
| PDF Assistant | Ask questions about PDFs |
| AI Agent | Automate multi-step workflows |
| Enterprise Search | Search across internal documents |
What You'll Learn in This Spring AI Series
- Spring AI Introduction
- Building Your First AI Chat Application
- Prompt Engineering
- AI Chat Memory
- Structured Output
- Function Calling
- Retrieval-Augmented Generation (RAG)
- Vector Databases
- AI Agents
- Production Best Practices
Summary
In this article, you learned:
- What Spring AI is
- Why it simplifies AI development
- Spring AI architecture
- Core components
- Supported AI providers
- How to build your first AI-enabled Spring Boot application
- Best practices for enterprise AI development
Spring AI brings the familiar Spring programming model to Generative AI, allowing Java developers to build intelligent applications without learning provider-specific SDKs.
In the next article, we'll build our first AI chatbot using Spring Boot and Spring AI.
Comments
Share a question, correction, or practical insight about this article.
Checking login status...
Loading approved comments...