A minimal Spring Boot + Spring AI application that exposes an HTTP endpoint backed by an OpenAI chat model.
- Java 21
- Spring Boot 4.0.1
- Spring AI 2.0.0
- Spring Web
- Spring AI OpenAI ChatClient
- Maven
- JDK 21 or newer
- Maven 3.9+
- An OpenAI API key
Set the API key as an environment variable.
export OPENAI_API_KEY="your-api-key"$env:OPENAI_API_KEY="your-api-key"Do not commit the API key to Git.
mvn clean spring-boot:runOpen:
http://localhost:8080/ai/ask?question=Explain%20Kafka%20consumer%20groups
Or with curl:
curl "http://localhost:8080/ai/ask?question=Explain%20Kafka%20consumer%20groups"HTTP Request
|
v
AiController
|
v
AiService
|
v
Spring AI ChatClient
|
v
OpenAI Chat Model
|
v
AI Response
The important Spring AI API is:
chatClient
.prompt()
.system("...")
.user(question)
.call()
.content();After this basic example works, the project can be extended with:
- Prompt templates
- Structured output
- Streaming responses
- Embeddings
- Vector database
- RAG
- Tool/function calling
- Conversation memory
- Observability