🔥 0
0
Lesson 1 of 8 20 min +150 XP

Why Spring AI for Java Devs

Your product manager walks over to your desk on a Monday morning. "Every competitor now has an AI shopping assistant. Customers type 'I need running shoes under 3000 rupees for flat feet' and get a real answer. We need one for ShopKart. How long?"

Five years ago your honest answer would have been: "We would need to hire a Python ML team, stand up a separate service, and learn a whole new stack." Today the answer is different. You already know Java. You already know Spring Boot. With Spring AI you can build that assistant inside the same codebase, using the same dependency injection, the same @RestController, and the same testing tools you use every day.

This course is built around one running project: the ShopKart AI shopping assistant. ShopKart is a fictional Indian e-commerce store that sells across categories, with prices in rupees (₹). By the final lesson you will have an assistant that can chat naturally, understand a shopper's intent, ground its answers in the real product catalog, and take actions like checking stock and placing orders.

Stay a Java dev, become an AI-capable Java dev

There is a myth that "AI = Python". Python dominates model training and research. But most companies are not training models. They are consuming models through an API and wiring the results into a real application: authentication, databases, transactions, caching, observability, deployment. That "real application" work is exactly what the Spring ecosystem has been perfecting for two decades.

Where the real work is Model training / research Python, GPUs, data science Done by a few labs Rarely what a company needs ~5% Building AI-powered apps APIs, DB, auth, transactions Caching, testing, deployment This is a Java/Spring job ~95%

You do not need to switch languages to do the 95%. You need a good abstraction over the model, and that is what Spring AI provides.

What is Spring AI?

Spring AI is an application framework, released as 1.0 GA in 2025, that brings AI model access into the Spring Boot programming model. Its guiding principle is portability: your application code talks to Spring abstractions, and the concrete provider (OpenAI, Anthropic Claude, Azure OpenAI, Google Vertex, Ollama for local models, and more) is chosen by configuration.

The abstractions you will use across this course:

AbstractionWhat it doesLesson
ChatClientFluent API to send prompts and get responses2, 3
Structured output (.entity())Map an LLM response straight into a Java record/POJO4
EmbeddingModelTurn text into vectors for semantic search5
VectorStoreStore and similarity-search embeddings5, 6
QuestionAnswerAdvisorRetrieval-Augmented Generation (RAG) in one line6
@ToolLet the model call your Java methods to take actions7
ChatMemoryRemember earlier turns in a conversation8

Because these are Spring beans, everything you already know applies: constructor injection, @Configuration, profiles, application.properties, @SpringBootTest, Actuator metrics.

The ShopKart domain

Every lesson builds toward the same assistant, so we fix the domain model once. Here are the core entities you will see throughout the course.

package com.shopkart.catalog;

import java.math.BigDecimal;
import java.util.List;

public record Product(
        Long id,
        String name,
        String category,
        String brand,
        BigDecimal price,     // in Indian Rupees
        double rating,        // 0.0 to 5.0
        int stock,
        String description,
        List<String> tags
) {}
package com.shopkart.customer;

public record Customer(
        Long id,
        String name,
        String email,
        String segment   // e.g. "NEW", "REGULAR", "PRIME"
) {}
package com.shopkart.order;

import java.math.BigDecimal;
import java.util.List;

public record OrderItem(Long productId, int quantity, BigDecimal unitPrice) {}

public record Order(
        Long id,
        Long customerId,
        List<OrderItem> items,
        BigDecimal total,
        String status   // "PLACED", "PACKED", "SHIPPED", "DELIVERED"
) {}

A sample product row so the numbers feel real:

new Product(
    101L,
    "Asics Gel-Contend 8 Running Shoes",
    "Footwear",
    "Asics",
    new BigDecimal("2799"),
    4.3,
    24,
    "Lightweight daily trainer with cushioned midsole, good for flat feet.",
    List.of("running", "shoes", "flat-feet", "under-3000")
);

The architecture we are building

Here is the shape of the finished ShopKart assistant. Do not worry about every box yet; each lesson lights up one part of this diagram.

ShopKart AI Assistant — target architecture Shopper web / app chat Spring Boot service @RestController + ChatClient LLM provider OpenAI / Anthropic Claude Prompt + persona system prompt (L3) VectorStore + RAG catalog (L5, L6) @Tool functions stock, orders (L7) ChatMemory + guardrails + observability multi-turn, safety, cost (L8)

Setting up the dependency

Spring AI 1.0 GA is on Maven Central. Start from a normal Spring Boot 3.x project (Java 17 or newer). First import the Spring AI BOM so all Spring AI artifacts share one version, then add a model starter.

<!-- pom.xml -->
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.ai</groupId>
      <artifactId>spring-ai-bom</artifactId>
      <version>1.0.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <!-- OpenAI is our default provider -->
  <dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-openai</artifactId>
  </dependency>
</dependencies>

The starter auto-configures a ChatModel, an EmbeddingModel, and a ChatClient.Builder bean for you. You just provide credentials and options in configuration:

# application.properties
spring.ai.openai.api-key=${OPENAI_API_KEY}
spring.ai.openai.chat.options.model=gpt-4o-mini
spring.ai.openai.chat.options.temperature=0.3
spring.ai.openai.embedding.options.model=text-embedding-3-small

Never hard-code the key. Read it from an environment variable (as above) or a secret manager.

Provider-agnostic by design

Spring AI's biggest payoff is that your business code does not change when you switch providers. Suppose ShopKart wants to evaluate Anthropic's Claude models alongside OpenAI. You swap the starter and the properties; the ChatClient code in your controllers stays identical.

<!-- Drop-in alternative provider -->
<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-starter-model-anthropic</artifactId>
</dependency>
# application.properties for Anthropic Claude
spring.ai.anthropic.api-key=${ANTHROPIC_API_KEY}
spring.ai.anthropic.chat.options.model=claude-sonnet-4-5

Same ChatClient, different backend. You can even keep both starters and select per environment with Spring profiles. That portability is exactly why an abstraction framework beats calling a provider SDK directly.

On the job

Teams that wrap the provider SDK directly get locked in. When pricing changes or a better model ships, migration becomes a rewrite. Adopting Spring AI early means model choice becomes a config decision your platform team can make, not an engineering project. Keep your API keys in a secret manager and pin the model id per environment so a silent upstream default change never surprises you in production.

Key takeaways

  • Most "AI work" in a company is application engineering, which is exactly what Java and Spring Boot excel at. You do not need to switch to Python.
  • Spring AI 1.0 GA brings AI into the Spring programming model with portable abstractions: ChatClient, EmbeddingModel, VectorStore, @Tool, ChatMemory.
  • Add the spring-ai-bom for version management, then a spring-ai-starter-model-* starter; the provider is chosen by configuration.
  • OpenAI is our default, but Anthropic Claude is a genuine drop-in: swap the starter and properties, keep your code.
  • Everything we build over the next seven lessons assembles into one deliverable: the ShopKart AI shopping assistant.

Next up: Talking to LLMs with ChatClient — your first working /assistant/ask endpoint.

🧠 Quick Quiz

Test your understanding of this lesson.

1

What is Spring AI's core value proposition for a Java developer?

2

Which starter dependency would you add to talk to OpenAI models?