July 1, 2026

RAG with Spring Boot — Embeddings and Vector Search Step by Step (2026)

Updated — July 1, 2026 · RAG fundamentals with Spring AI and vector search.

Kindson Munonye · Software engineer & technical author
GitHub · LinkedIn · About · YouTube
Last updated by Kindson Munonye — July 1, 2026


📚 Tutorial hubs:
AI Developer Tutorials ·
Spring Boot ·
Angular ·
CRUD + REST guide

Source code: munonye-ai-chat-spring-angular on GitHub

Estimated reading time: 12–15 minutes · Last updated: July 1, 2026


Learn how to build a RAG Spring Boot tutorial pipeline that answers questions from your own documents. This post extends the AI Developer Tutorials series and connects to M7-A Spring AI REST basics.

RAG architecture

Documents → chunk → embed → VectorStore
User question → embed → top-K similar chunks → prompt → LLM → answer

Dependencies

<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-pgvector-store-spring-boot-starter</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

Ingestion service

@Service
public class DocumentIngestionService {
  private final VectorStore vectorStore;
  private final Resource docsFolder;

  public DocumentIngestionService(VectorStore vectorStore,
      @Value("classpath:docs/") Resource docsFolder) {
    this.vectorStore = vectorStore;
    this.docsFolder = docsFolder;
  }

  public void ingestAll() throws IOException {
    for (Resource file : docsFolder.getFile().listFiles()) {
      String text = Files.readString(file.getFile().toPath());
      List<Document> chunks = split(text, 800, 100);
      vectorStore.add(chunks);
    }
  }

  private List<Document> split(String text, int size, int overlap) {
    List<Document> out = new ArrayList<>();
    for (int i = 0; i < text.length(); i += size - overlap) {
      out.add(new Document(text.substring(i, Math.min(i + size, text.length()))));
    }
    return out;
  }
}

Question endpoint

@PostMapping("/api/ask")
public AnswerResponse ask(@RequestBody QuestionRequest req) {
  List<Document> similar = vectorStore.similaritySearch(req.question(), 5);
  String context = similar.stream().map(Document::getContent).collect(Collectors.joining("\n---\n"));
  String answer = chatClient.prompt()
      .system("Answer only from the context below. Say 'I don't know' if not found.\n" + context)
      .user(req.question())
      .call()
      .content();
  return new AnswerResponse(answer);
}

Next: function calling from Angular

M8-B — Structured JSON from LLMs in Angular

Related:
AI Developer Tutorials hub ·
Angular CRUD Part 1 ·
Spring AI overview

Kindson Munonye

Kindson Munonye is a software engineer and technical author specializing in Angular, Spring Boot, and microservices architecture. He publishes step-by-step tutorials with source code covering CRUD operations, reactive forms, CQRS, event sourcing, and REST API integration.GitHub · LinkedIn · About · YouTube

View all posts by Kindson Munonye →
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted