Getting started with Spring AI and Azure OpenAI


Introduction
In this article we look at how to get started with Spring AI project and connect to Azure OpenAI Large Language Models (LLMs).
Feel free to browse other articles in this getting started with Spring AI series:
Create a simple Spring Application (Beginners)
We can use Spring Initializr to generate a skeleton project for our simple application. You can do that either in IDE itself, or directly with a pre-configured project (e.g. Maven, Java, Spring Boot 3.4.4, spring-ai-demo, com.example package name. JAR packaging, Java 24 with Spring Web).
Clicking on “Generate” button will download a project skeleton zip archive, e.g. spring-ai-demo.zip
which you can unzip and open in editor of your choice (e.g. IntelliJ, VS Code, etc.)
We can start adding specific AI model Spring Boot starters later on.
Let’s add a HomeController
, e.g.
package com.example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@GetMapping("/")
public String home() {
return "Hello World!";
}
}
Let’s run our simple application, e.g.
./mvnw spring-boot:run
Feel free to browse from another terminal window, e.g. http localhost:8080
http localhost:8080
HTTP/1.1 200
Connection: keep-alive
Content-Length: 12
Content-Type: text/plain;charset=UTF-8
Date: Tue, 11 Apr 2025 08:00:00 GMT
Keep-Alive: timeout=60
Hello World!
or a directly in the browser at http://localhost:8080
Feel free to explore Spring Boot Getting Started Guide for more details on how to create a simple Spring Boot web application.
Connecting your Spring Application to Azure OpenAI models
Configure OpenAI
In this section we will explore connecting your application to Azure OpenAI Large Language Models (LLMs). If you don’t already have an account on Azure, please sign up for an account, e.g.
Once you have created, authenticated and configured your Azure account, you can select Azure OpenAI from the Azure Marketplace to create an instance of Azure OpenAI, e.g.
Within a minute or so, your deployment will be ready. You should be able to find your key and your endpoint, e.g.
Please save the key (e.g. Ddlvau6w2KHzFC...
) and endpoint (e.g. https://nevenc.openai.azure.com
). Keep these secure, because anyone with your API key can access and consume your Azure OpenAI API platform credits.
Our Azure OpenAI instance does not have any models deployed, so let’s add a model we can use in your application. You need to go to Azure AI Foundry portal
from your Azure OpenAI Summary page, and create a new deployment, e.g.
Fill in the details for the deployment name that we will use in our application later, e.g. azure-gpt-4o
Finally, let’s add Spring AI code to start using OpenAI API.
Add Spring AI for OpenAI
We will add Spring AI Spring Boot Starter for OpenAI in our Maven pom.xml
, e.g.
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-azure-openai</artifactId>
<version>1.0.0-M7</version>
</dependency>
Notice, we are using version 1.0.0-M7
that’s newest at the time of writing this article. Please replace with the latest Spring AI version.
We also need to add few Spring properties to our src/main/resources/application.properties
file, e.g.
spring.application.name=spring-ai-demo
spring.ai.azure.openai.api-key=Ddlvau6w2KHzFC...
spring.ai.azure.openai.endpoint=https://nevenc.openai.azure.com
spring.ai.azure.openai.chat.options.deployment-name=azure-gpt-4o
Notice, we have used azure-gpt-4o
deployment that uses gpt-4o
large language model (LLM) for this simple use case. Feel free to explore all Azure OpenAI Service models. These models vary in size, speed, training data and eventually in price per 1M tokens. For example, gpt-4o-mini
model costs per 1M tokens are: (1) for input US$0.15, (2) for cached input US$0.075 and (3) for output US$0.60, whereas gpt-4o
model costs per 1M tokens are: (1) for input US$2.50, (2) for cached input US$1.25 and (3) for output US$10.00.
Finally, let’s add a call to a LLM from our HomeController
, e.g.
package com.example;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
private ChatClient chatClient;
public HomeController(ChatClient.Builder builder) {
this.chatClient = builder.build();
}
@GetMapping("/")
public String home() {
return chatClient
.prompt()
.user("who are you")
.call()
.content();
}
}
Run the application again, e.g.
./mvnw spring-boot:run
Test the application, in another terminal or browser, e.g.
http localhost:8080
HTTP/1.1 200
Connection: keep-alive
Content-Length: 12
Content-Type: text/plain;charset=UTF-8
Date: Tue, 11 Apr 2025 08:05:00 GMT
Keep-Alive: timeout=60
Hello! I'm OpenAI's ChatGPT, an advanced AI language model designed to assist with
information, answer questions, and provide guidance on a wide range of topics.
Think of me as a helpful virtual assistant or conversational partner.
How can I assist you today? 😊
Congratulations! You have created your first application that talks to an Azure OpenAI service.
Possible Gotcha’s
If you run into ResourceNotFoundException
(see below example) - this is probably because you have misconfigured Azure OpenAI instance, mistyped your deployment name, or have just created it. Maybe wait for few minutes and try running your application again. If the problem persists, please check your deployment name in the Azure AI Foundry (Azure Portal).
com.azure.core.exception.ResourceNotFoundException: Status code 404,
"
{
"error": {
"code":"DeploymentNotFound",
"message": "The API deployment for this resource does not exist.
If you created the deployment within the last 5 minutes,
please wait a moment and try again."
}
}
"
Similarly, you might run into ClientAuthenticationException
(see below example) - this is probably because you did not properly copied your Azure OpenAI API key, please check your key in Azure Portal.
com.azure.core.exception.ClientAuthenticationException: Status code 401,
"
{
"error": {
"code":"401",
"message": "Access denied due to invalid subscription key or wrong API endpoint.
Make sure to provide a valid key for an active subscription and use
a correct regional API endpoint for your resource."
}
}
"
Hope you enjoyed this beginners getting started with Spring AI article!
Connecting to other LLMs
If you want to run your Spring AI application with other LLM models, look at our articles:
References
Please explore other Getting started with Spring AI articles in this series:
Subscribe to my newsletter
Read articles from Neven Cvetkovic directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
