Spring AI Introduction — Build AI-Powered Java Apps with Spring Boot
Spring AI is an official Spring project that brings Artificial Intelligence capabilities into the Spring ecosystem. It provides a consistent, portable API to work with large language models (LLMs), embedding models, vector stores, and AI tools — the same way Spring Data provides a consistent API for databases. You write your code once and swap providers (OpenAI, Anthropic, Ollama, Google Gemini) by changing configuration.
This tutorial sets up a Spring Boot project with Spring AI, calls an LLM, and prints a response. By the end you will have a working AI-powered REST endpoint.
What Spring AI Provides
Spring AI
├── Chat Models → OpenAI, Anthropic Claude, Ollama, Google Gemini, Azure OpenAI
├── Embedding Models → Convert text to vectors for semantic search
├── Vector Stores → PGVector, ChromaDB, Pinecone, Redis
├── Document Readers → PDF, HTML, Text, Markdown loaders
├── Prompt Templates → Dynamic prompt construction
├── Output Parsers → Map AI response → Java object / List / Map
├── Tool/Function Call → Give AI access to your Java methods
└── MCP Support → Model Context Protocol server and client
Maven Dependencies (pom.xml)
<properties>
<java.version>17</java.version>
<spring-ai.version>1.0.0</spring-ai.version>
</properties>
<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>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring AI OpenAI starter -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
</dependencies>
application.properties
spring.ai.openai.api-key=${OPENAI_API_KEY}
spring.ai.openai.chat.options.model=gpt-4o-mini
spring.ai.openai.chat.options.temperature=0.7
Set the environment variable before running: set OPENAI_API_KEY=sk-... on Windows or export OPENAI_API_KEY=sk-... on Linux/Mac.
AiService.java
package com.java9r.ai;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.stereotype.Service;
@Service
public class AiService {
private final ChatClient chatClient;
// Spring AI auto-configures ChatClient.Builder — inject and build here
public AiService(ChatClient.Builder builder) {
this.chatClient = builder.build();
}
public String ask(String question) {
return chatClient.prompt()
.user(question)
.call()
.content();
}
}
AiController.java
package com.java9r.ai;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/ai")
public class AiController {
private final AiService aiService;
public AiController(AiService aiService) {
this.aiService = aiService;
}
@GetMapping("/ask")
public String ask(@RequestParam String q) {
return aiService.ask(q);
}
}
Main Application Class
package com.java9r.ai;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringAiApp {
public static void main(String[] args) {
SpringApplication.run(SpringAiApp.class, args);
}
}
Test the Endpoint
GET http://localhost:8080/ai/ask?q=What+is+Spring+AI+in+one+sentence
Output
Spring AI is a Spring Framework project that provides a unified, portable API
for integrating AI capabilities — such as chat, embeddings, and function calling
— into Java applications using familiar Spring patterns.
How ChatClient Works
ChatClient.Builder (auto-configured by Spring AI)
↓
chatClient.prompt() → start building a prompt
.system("You are ...") → optional system message
.user("question") → user message
.call() → send to AI model
.content() → get string response
OR
.chatResponse() → get full ChatResponse with metadata
.entity(MyClass.class) → parse response into a Java object
Key Points
- Spring AI auto-configures
ChatClient.Builder— inject it, call.build()once per service - The
modelandtemperatureset inapplication.propertiesare defaults; you can override per-call - Switching from OpenAI to another provider only requires changing the starter dependency and properties — your service code stays the same
- Spring AI requires Java 17+ and Spring Boot 3.2+
Comments