October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk6 min

Spring AI RAG Tutorial with Spring Boot (2.0.1)

A version-pinned guide to ingesting your documents into a Spring AI VectorStore and using QuestionAnswerAdvisor or RetrievalAugmentationAdvisor to retrieve context for a Spring Boot chat app.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

This tutorial builds a Spring Boot question-answering flow that retrieves relevant passages from your own documents and supplies them to a chat model. It targets Spring AI 2.0.1, the release listed in the current API overview. The exact Spring Boot version and model or vector-store provider depend on the integrations you choose, so use dependencies and configuration documented for the same Spring AI release rather than mixing examples across versions. See the Spring AI API overview and upgrade notes.

How the RAG flow works

Retrieval-augmented generation (RAG) separates document lookup from answer generation. During ingestion, the application turns source content into Spring AI Document objects and adds them to a VectorStore. At question time, the application searches that store for relevant documents and supplies retrieved text as context to the chat model. Spring AI describes RAG as a way to address large language models’ difficulties with long-form content, factual accuracy, and context-awareness in its retrieval-augmented generation reference.

The vector store is an abstraction, not a database that is selected for you. Choose and configure an implementation that fits your deployment, persistence, metadata-filtering, and operational requirements. Spring AI documents multiple integrations in its vector database reference.

Set up a version-consistent project

Use Spring AI 2.0.1 for the API names and module names in this tutorial. The model integration, embedding model, vector-store implementation, and Spring Boot version are choices you must make for your project; the Spring AI overview documents starters and Boot auto-configuration, but there is no single provider-specific dependency set that fits every application.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Choose a Spring AI chat-model integration and configure its credentials and connection settings.
  • Choose an embedding-model integration. Ingestion and query retrieval must use compatible embeddings for meaningful similarity search.
  • Choose a vector-store integration and configure its connection, persistence, and any required schema or collection.
  • Add the current advisor module, spring-ai-vector-store-advisor, for the simple question-answer flow. The modular flow later in this tutorial uses spring-ai-rag.

Check each starter’s coordinates and configuration against the release documentation before adding it to Maven or Gradle. Spring AI 2.0 changed some names from 1.1.x, including the vector-store advisor module; the upgrade notes explain those changes. Do not copy a dependency name from an older example and assume it is current.

Prepare and ingest documents

Ingestion is a separate application step from answering questions. For a small example, create documents from safe-to-share text you control, attach useful metadata, and add them to the configured store. For example, the central operation looks like this once a VectorStore is available:

import java.util.List;
import java.util.Map;

import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.VectorStore;

List<Document> documents = List.of(
    new Document(
        "A refund request must be submitted within 30 days of purchase.",
        Map.of("source", "returns-policy", "section", "refunds")
    ),
    new Document(
        "Items marked final sale are not eligible for a refund.",
        Map.of("source", "returns-policy", "section", "exceptions")
    )
);

vectorStore.add(documents);

This snippet assumes that vectorStore is already configured through your selected Spring AI integration. The text and metadata are illustrative. In a real application, load documents from your chosen source and preserve metadata—such as document identifier, source, access scope, or section—that can support filtering and traceability later.

For larger files or formats such as PDFs, use an appropriate reader or extraction process to turn source material into text and then into documents. A reader does not mean every format is automatically ingested. Split long content into smaller pieces when appropriate so retrieval can select relevant passages rather than returning an entire source. Spring AI’s vector-store documentation covers preparing Document records, loading data, and calling the store’s add operation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Ask a question with QuestionAnswerAdvisor

For a direct vector-store question-answer pattern, attach a QuestionAnswerAdvisor to a ChatClient. The advisor performs similarity search and augments the prompt with retrieved context.

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor;
import org.springframework.ai.vectorstore.VectorStore;

ChatClient chatClient = ChatClient.builder(chatModel)
    .defaultAdvisors(new QuestionAnswerAdvisor(vectorStore))
    .build();

String answer = chatClient.prompt()
    .user("What is the refund window, and are final-sale items eligible?")
    .call()
    .content();

Here, chatModel represents the chat model supplied by your chosen Spring AI model integration. This example requires the current spring-ai-vector-store-advisor module as well as the selected chat-model and vector-store integrations. Consult the RAG reference for the advisor’s current API and supported options.

The advisor helps retrieve and pass context; it does not guarantee that the response will be correct, complete, or supported by the retrieved passages. Evaluate the application against your own questions and documents, and decide how the user experience should behave when search returns no useful material.

Use RetrievalAugmentationAdvisor for a modular flow

When retrieval needs to be composed with query transformation or document processing, use RetrievalAugmentationAdvisor rather than treating the direct question-answer advisor as the only option. Spring AI’s modular approach separates retrieval from other steps, allowing the flow to be configured for the use case. The module documented for this approach is spring-ai-rag.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.rag.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;

var retriever = VectorStoreDocumentRetriever.builder()
    .vectorStore(vectorStore)
    .build();

var ragAdvisor = RetrievalAugmentationAdvisor.builder()
    .documentRetriever(retriever)
    .build();

ChatClient chatClient = ChatClient.builder(chatModel)
    .defaultAdvisors(ragAdvisor)
    .build();

The snippet shows the shape of the modular flow: a vector-store document retriever is supplied to a retrieval-augmentation advisor, which is then attached to the chat client. Exact builder options and imports should be checked against the 2.0.1 reference for the modules you have added. The RAG documentation also describes query transformers and document post-processors, including reranking and removing irrelevant or redundant results.

Tune what reaches the model

Retrieval options affect which evidence is available to the model. Treat settings as hypotheses to test on your corpus, not universal defaults.

  • Top-k: Controls how many matches are retrieved. Raising it can surface more relevant passages, but can also add irrelevant text and consume more prompt context.
  • Similarity threshold: Excludes results below a relevance cutoff. The useful cutoff depends on the data and retrieval implementation; do not assume one value works across stores or corpora.
  • Metadata filters: Restrict which documents are eligible, for example by source or other metadata. Spring AI documents both configured and runtime filtering options.
  • Query transformation: Rewriting or expanding an ambiguous or conversational question can improve the search query’s usefulness, but adds processing to the flow.
  • Post-processing: Reranking, removing duplicates, or compressing retrieved material can improve the context presented to generation, but requires evaluating the results for your task.

Spring AI documents these controls in its RAG reference and vector-store guide. The documented controls do not establish a universal benchmark setting or guarantee better answers; judge them with representative questions, expected evidence, and failure cases from your own application.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Decide what happens when retrieval is weak

For the modular RetrievalAugmentationAdvisor flow, the reference says the default behavior does not allow empty retrieved context and instructs the model not to answer in that situation. It also documents an option to allow empty context. Choose deliberately: a policy that declines to answer without retrieved material is different from one that lets the model respond without it. Test the behavior when the store has no matching document, only weak matches, or conflicting passages, and make the application’s response understandable to users.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a vector store for the application

Spring AI’s VectorStore interface gives the application a common API, but integrations can differ in capabilities and operational fit. Compare candidate implementations on the points that matter to your workload:

  • Whether the integration is supported for the Spring AI release you are using.
  • Where the store runs and who handles deployment, upgrades, backups, and monitoring.
  • Whether its metadata filters and persistence behavior fit the application.
  • Whether it meets your project’s security, data-residency, and operational constraints.

The official material cited here does not establish a best provider, comparative performance result, or pricing recommendation. Review the provider integration documentation alongside Spring AI’s vector-store reference and API overview before committing to a deployment choice.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.