Getting started with Spring AI — Get your first model response with ChatClient
On this page
Send a question to a model when a Spring Boot app starts and print the reply to the terminal. With Spring AI’s ChatClient, you can assemble messages in Java and extract the text you need from the response. This exercise starts with a console app that runs without a web server or database. Spring AI ChatClient documentation
1. Match your Spring Boot and Spring AI versions
This example uses Java 21, Spring Boot 4.2.0, Spring AI 2.1.0, and Maven. Spring AI 2.1 is built on Spring Boot 4.2. The spring-ai-bom manages Spring AI library versions, so the OpenAI starter needs no version of its own. Spring AI 2.1.0 release announcement · OpenAI starter documentation
Create a pom.xml in an empty directory.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.2.0</version>
</parent>
<groupId>example</groupId>
<artifactId>spring-ai-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>21</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>2.1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. Configure the model and API key
Create src/main/resources/application.properties. Refer to the environment variable in the configuration file and supply the actual key from the terminal where you run the app. This example uses gpt-5-mini, which supports Chat Completions. Spring AI OpenAI configuration · GPT-5 mini model documentation
spring.ai.openai.api-key=${OPENAI_API_KEY}
spring.ai.openai.chat.model=gpt-5-mini
A live call requires an OpenAI API key and an API project with access to the model. API usage may incur charges. OpenAI API quickstart
3. Send a question with ChatClient
Create src/main/java/example/SpringAiHelloApplication.java. Inject the ChatClient.Builder that Spring Boot auto-configures, then use a CommandLineRunner to call the model once at startup. Spring AI ChatClient documentation
The diagram shows a successful request. The table at the end covers startup and API errors.
package example;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SpringAiHelloApplication {
public static void main(String[] args) {
SpringApplication.run(SpringAiHelloApplication.class, args);
}
@Bean
CommandLineRunner ask(ChatClient.Builder builder) {
ChatClient chat = builder.build();
return args -> {
String answer = chat.prompt()
.system("Answer in English in one sentence.")
.user("Explain what Spring Boot auto-configuration is.")
.call()
.content();
System.out.println("Model response: " + answer);
};
}
}
Use system() for instructions such as the reply format and user() for the question itself. call().content() calls the model synchronously and returns the response text as a string. The OpenAI starter used in this example turns the messages into a Chat Completions request. ChatClient call methods · OpenAI Chat configuration
4. Run the app and check the result
Build from the directory containing pom.xml. The commands below are for Bash or Zsh. Entering the key with read -rs keeps it off the screen.
mvn -B package
printf 'Enter your OpenAI API key: '
read -rs OPENAI_API_KEY
printf '\n'
OPENAI_API_KEY="$OPENAI_API_KEY" java -jar target/spring-ai-hello-0.0.1-SNAPSHOT.jar
unset OPENAI_API_KEY
On success, the app prints the answer after Model response:. The wording may vary between runs.
Model response: Spring Boot auto-configuration automatically configures the beans an application needs based on its classpath libraries and settings.
| Symptom | What to check |
|---|---|
Startup error saying OPENAI_API_KEY could not be found |
Run the same command from the terminal where you entered the key. |
API returns 401 |
Check that the key is valid and was issued for your API project. |
API returns 429 |
Read the response error message to see whether you hit a request rate limit or an API usage quota. |
What was verified. The example was built with Java 21.0.1 and Maven 3.9.5 and run against a local OpenAI-compatible mock server. The app sent the model name and the system and user messages to /v1/chat/completions, printed the mock response to the terminal, and exited normally. The sentence above illustrates the output format. No live OpenAI API call was made.
Discussion