DEV Community

Cover image for Building AI Applications with Spring AI and Azure OpenAI
Ayush Shrivastava
Ayush Shrivastava

Posted on

Building AI Applications with Spring AI and Azure OpenAI

Artificial Intelligence is no longer optional for modern applications. Whether you're building a chatbot, developer assistant, FAQ engine, or content generator — AI integration is becoming a core backend requirement.

If you're already a Spring Boot developer, the good news is: you don’t need to learn an entirely new ecosystem to build AI-powered apps.

Thanks to Spring AI, integrating models like Azure OpenAI Service becomes clean, structured, and production-friendly.

In this blog, I’ll walk you step-by-step through building a simple AI-powered REST API using Spring AI and Azure OpenAI.

1. What is Spring AI?

Spring AI is a Spring ecosystem project designed to simplify AI integration in Spring Boot applications.

Instead of manually:

  • Writing HTTP calls
  • Handling authentication headers
  • Parsing JSON responses
  • Managing model-specific configurations

Spring AI provides clean abstractions like:

  • ChatClient
  • EmbeddingClient
  • Model abstraction layer
  • Auto configuration support

If you already know Spring Boot, you can start building AI applications almost immediately.

2. Why Do We Need Spring AI?

Before Spring AI

Developers had to:

  • Manually call REST APIs
  • Handle authentication tokens
  • Build request bodies
  • Parse large AI responses
  • Maintain provider-specific logic

This made code:

  • Hard to maintain
  • Hard to scale
  • Difficult to switch AI providers

With Spring AI

  • Clean dependency injection
  • Simple configuration
  • Provider abstraction
  • Spring Boot auto configuration
  • Production-ready structure

AI calls now look like standard Spring code.

That’s powerful.

3. Core Features of Spring AI

ChatClient

Used for sending prompts and receiving AI-generated responses.

Model Abstraction

Switch between providers (OpenAI, Azure OpenAI, etc.) without rewriting business logic.

Auto Configuration

Works seamlessly with Spring Boot auto configuration.

** Simple Prompt Handling**

Send a message and receive output in just a few lines of code.

Integration Friendly

Works smoothly with:

  • REST APIs
  • Databases
  • Microservices
  • Cloud deployments

4. Project Setup Overview

Let’s understand the project architecture step by step.

We are building:

  • A Spring Boot application
  • Connected to Azure OpenAI
  • Exposing a REST endpoint
  • Secured using environment variables

5. Maven Dependencies Explained

Spring Boot Parent

Provides dependency management and plugin configuration.

Spring Web MVC

Used to build REST APIs.

Spring AI Azure OpenAI Starter

Connects your application with Azure OpenAI service.

Spring AI BOM

Manages compatible versions of Spring AI libraries.

DevTools

Used for development-time restart.

** Test Dependency**

Used for writing unit and integration tests.

6. application.properties Explained

`spring.application.name=openai

spring.ai.azure.openai.api-key=${AZURE_OPENAI_API_KEY}
spring.ai.azure.openai.endpoint=${AZURE_OPENAI_ENDPOINT}
spring.ai.azure.openai.chat.options.deployment-name=${AZURE_OPENAI_DEPLOYMENT}
spring.ai.azure.openai.chat.options.temperature=1
`

Explanation

  • application.name → Name of your application
  • api-key → Azure OpenAI API key (from environment variable)
  • endpoint → Azure endpoint URL
  • deployment-name → Your deployed model name
  • temperature → Controls AI creativity

Using environment variables is a secure way to manage secrets.

Never hardcode your API keys.

7. Main Application Class

`@SpringBootApplication
public class OpenaiApplication {

public static void main(String[] args) {
    SpringApplication.run(OpenaiApplication.class, args);
}
Enter fullscreen mode Exit fullscreen mode

}
`

Explanation

  • @SpringBootApplication → Enables auto configuration & component scanning
  • SpringApplication.run() → Starts the application

This is the entry point of your backend.

8. Chat Controller Explained

`@RestController
@RequestMapping("/api")
public class ChatController {

private final ChatClient chatClient;

public ChatController(ChatClient.Builder chatClientBuilder) {
    this.chatClient = chatClientBuilder.build();
}

@GetMapping("/chat")
public String chat(@RequestParam("message") String message) {
    return chatClient
            .prompt(message)
            .call()
            .content();
}
Enter fullscreen mode Exit fullscreen mode

}
`

@RestController

Marks the class as a REST controller.

@RequestMapping("/api")

Base URL path.

ChatClient Injection

Spring automatically provides ChatClient.Builder.

We build it using:

chatClientBuilder.build();

/chat Endpoint

Example:

http://localhost:8080/api/chat?message=Hello

Final Summary

Using:

  • Spring Boot
  • Spring AI
  • Azure OpenAI

You created an intelligent backend system with minimal code.

Spring AI removes complexity and makes AI integration feel native to Spring developers.

This is the modern way of building intelligent backend applications using Java.

Source Code

You can find the complete project here:

GitHub Repository:

GitHub logo ayushstwt / spring-ai-azure-openai-chat-api

Demo project for Spring Ai and OpenAI

Building AI Applications with Spring AI and Azure OpenAI

Author: Ayush Shrivastava


1. What is Spring AI?

Spring AI is a Spring ecosystem project that helps developers build AI-powered applications using simple and familiar Spring Boot concepts.

It provides integration with popular AI providers like OpenAI and Azure OpenAI. Instead of writing complex HTTP calls manually, Spring AI gives you ready-to-use abstractions like ChatClient, EmbeddingClient, and more.

If you already know Spring Boot, you can easily start building AI applications without learning new complicated frameworks.


2. Why Do We Need Spring AI?

Before Spring AI: - Developers had to manually call REST APIs. - They had to handle authentication, request bodies, and response parsing. - Code became complex and hard to maintain.

With Spring AI: - Configuration is simple. - Dependency injection works as usual. - AI calls look clean and readable. - It integrates smoothly with Spring Boot projects.

Top comments (0)