DEV Community

Adnene HAMDOUNI
Adnene HAMDOUNI

Posted on

Spring AI: Integrating Artificial Intelligence into Your Java Applications in Minutes

Spring AI: Integrating Artificial Intelligence into Your Java Applications in Minutes


📌 Metadata

  • Subject: Spring Boot / AI / Java
  • Target: Java Developers, Software Architects
  • Estimated Reading Time: 8 minutes
  • Tags: #SpringBoot #SpringAI #Java #LLM #GenerativeAI

🎯 Introduction

For a long time, generative AI seemed to be the exclusive domain of Python. Java developers were often forced to call raw REST APIs without any real abstraction layer.

That's where Spring AI comes in.

This new project from the Spring ecosystem doesn't just add an HTTP client for OpenAI. It brings a real abstraction layer, similar to what Spring Data did for databases. The idea is simple: you write your business logic once, and you can change your AI model (GPT-4, Claude, Mistral, or even a local model via Ollama) without modifying a single line of business code.

By the end of this article, you will know how to configure your first Spring AI project and create a chat service capable of returning structured data.


🛠️ Technical Core

1. Fundamental Concept: Model Abstraction

At the heart of Spring AI is the ChatModel interface. It standardizes interactions with LLMs. This is critical in an enterprise environment to avoid "vendor lock-in". You can switch from OpenAI to a local model via Ollama simply by changing a dependency.

2. Technical Implementation

Prerequisites

  • JDK 17 or higher.
  • Spring Boot 3.x.
  • An OpenAI API key (or Ollama installed locally).

Step 1: Project Configuration

Add the corresponding starter to your pom.xml:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Then, configure your API key in the application.yml file:

spring:
  ai:
    openai:
      api-key: ${OPENAI_API_KEY}
      chat:
        options:
          model: gpt-4o
          temperature: 0.7
Enter fullscreen mode Exit fullscreen mode

Step 2: Creating the Chat Service with ChatClient

The fluent ChatClient API is the modern way to interact with AI. Here's how to implement a simple controller:

@RestController
@RequestMapping("/ai")
class AiController {
    private final ChatClient chatClient;

    public AiController(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }

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

Step 3: Mastering PromptTemplates and Structured Output

To make your applications truly useful, you cannot rely on free-text messages. You need structures.

PromptTemplates allow you to inject dynamic variables:

String response = chatClient.prompt()
    .user(u -> u
        .text("Explain the concept of {concept} in two sentences for a beginner.")
        .param("concept", "Dependency Injection"))
    .call()
    .content();
Enter fullscreen mode Exit fullscreen mode

Structured Output is the "killer feature" for Java devs. You can map the AI response directly into a Java record:

record Movie(String title, String director, int year) {}

@GetMapping("/movie-info")
public Movie getMovieInfo(@RequestParam String movieName) {
    return chatClient.prompt()
        .user("Give me info on the movie " + movieName)
        .call()
        .entity(Movie.class);
}
Enter fullscreen mode Exit fullscreen mode

Pro Tip: Use Advisors (like MessageChatMemoryAdvisor) to natively add conversation history without having to manually manage a list of messages for every call.

3. Analysis & Comparison: Spring AI vs LangChain4j

If you explore AI in Java, you will definitely encounter LangChain4j.

Criterion Spring AI LangChain4j
Integration Native and deep with Spring Boot Standalone libraries (usable everywhere)
Learning Curve Very low for Spring devs Moderate (closer to LangChain Python)
Ecosystem Benefits from the entire Spring ecosystem Very complete on third-party integrations
Philosophy Abstraction and simplicity Flexibility and functional richness

Verdict: If your stack is already Spring Boot, Spring AI is the logical choice for its simplicity and alignment with your development patterns.


🏁 Conclusion & Opening

AI is no longer an "option" or a gadget for data scientists; it is now a first-class citizen in the Java ecosystem. With Spring AI, the technical barrier collapses to make room for business innovation.

But a LLM alone has a limit: it doesn't know your private data. That's where RAG (Retrieval Augmented Generation) comes in. In the next article, we'll see how to give a "memory" to your Spring Boot application by connecting a Vector Store.

Your turn! Install Ollama locally, configure Spring AI, and try creating your first structured agent. Share your feedback in the comments!


📚 Sources & Resources

Top comments (0)