Getting Started with Spring AI for Java Developers
Artificial intelligence is advancing rapidly, but integrating AI capabilities into Spring-based Java applications has traditionally been challenging. Spring AI simplifies this process by utilizing familiar Spring conventions such as dependency injection and configuration-driven development. In this tutorial, you’ll learn how to incorporate AI features into your Spring applications, starting with a basic example that interfaces with OpenAI. We’ll also explore how to implement prompt templates for user queries and introduce retrieval augmented generation (RAG) using a vector store to manage external documents.
What is Spring AI?
Spring AI emerged as a project in 2023, with its initial milestone released in early 2024. The stable, production-ready Spring AI 1.0 was officially launched in May 2025. It provides an abstraction layer for interacting with large language models (LLMs), similar to how Spring Data abstracts database access. Spring AI offers tools for managing prompts, selecting models, and handling AI responses, with support for multiple providers such as OpenAI, Anthropic, Hugging Face, and Ollama for local deployment. Switching between providers is straightforward—just update your configuration settings.
Developers configure AI resources through application.yaml or application.properties files, inject Spring beans that conform to standard interfaces, and write code against those interfaces. Spring AI then manages the underlying interactions with various AI models seamlessly.
Building a Spring Application to Query OpenAI
Let’s create a simple Spring MVC application that exposes an endpoint to send questions directly to OpenAI. You can download the sample code or generate a new project at start.spring.io. When selecting dependencies, be sure to include “Spring Web” and “OpenAI” from the AI section.
Start by configuring your AI provider in an application.yaml file. Here’s an example configuration:
spring:
application:
name: spring-ai-demo
ai:
openai:
api-key: ${OPENAI_API_KEY}
chat:
options:
model: gpt-5
temperature: 1
In this configuration, the API key is fetched from the environment variable OPENAI_API_KEY, so make sure to set this variable before running your application. The configuration specifies the model to use—here, GPT-5—and other options like temperature for response variability.
Spring AI handles the details of interacting with OpenAI models, allowing you to focus on building your application’s logic. You can easily switch models or providers by updating your configuration settings.












What do you think?
It is nice to know your opinion. Leave a comment.