# Building Spring by Example V2: Completing Module 10 - Spring AI

> Source: <https://dev.to/iammujuzimoses/building-spring-by-example-v2-completing-module-10-spring-ai-42a7>
> Published: 2026-11-26 08:07:51+00:00

**Recommendation:** Mock ChatModel responses and assert on constructed prompts to test Spring AI services without hitting a provider, and switch providers via Spring profiles instead of code changes.
More in the playbook "Evals and testing for LLM apps": <https://wpnews.pro/playbooks/evals-testing>

When I started learning Spring, I noticed something about many of the tutorials I found.

They often showed how to use a feature, but not always why it worked or when you should choose one approach over another.

I learn best by building things and documenting what I discover, so I started a project called **spring-by-example**.

The idea is simple: instead of building one large application, I’m creating a collection of small, focused examples where each example explores a specific Spring concept with clear explanations, tests, and documentation.

I’m happy to say that I’ve now completed **Module 10 - Spring AI**. 🎉

This module introduced me to building AI-powered applications with Spring AI, from interacting with language models to working with embeddings, vector stores, retrieval-augmented generation, and AI tools.

The module covers:

One thing I really enjoyed about this module was discovering how Spring AI provides familiar Spring abstractions for working with AI models while hiding much of the complexity involved in communicating with different providers.

I started with the fundamentals and chat models, exploring how an application can send messages to a language model and retrieve responses using `ChatClient` and `ChatModel`.

From there, I explored prompt templates.

Instead of hardcoding prompts directly into application logic, I learned how to externalize them and provide dynamic values using placeholders.

For example:

```
PromptTemplate promptTemplate = new PromptTemplate(
        "Explain {technology} to {audience}."
);

Prompt prompt = promptTemplate.create(Map.of(
        "technology", "Spring Boot",
        "audience", "Java developers"
));
```

Structured output introduced another interesting concept.

Rather than working with unstructured text responses, I explored how Spring AI can convert model responses into Java objects using `BeanOutputConverter`.

This makes it easier to work with predictable response structures in application code.

Embeddings and vector stores were another important part of the module.

I explored how text can be converted into numerical vector representations that capture semantic meaning.

This led to vector stores, where I learned how to store documents and retrieve relevant information through similarity searches.

One of the most interesting examples was **Retrieval-Augmented Generation (RAG)**.

RAG combines information retrieval with language model generation.

Instead of relying entirely on the model's existing knowledge, an application can retrieve relevant documents and provide them as context when generating an answer.

The simplified flow looks like this:

```
User Question
      |
      v
Vector Similarity Search
      |
      v
Retrieve Relevant Documents
      |
      v
Build Prompt with Context
      |
      v
Language Model
      |
      v
Generated Answer
```

This helped me understand how AI applications can provide answers grounded in specific information sources.

I also explored **Tool Calling**.

This was particularly interesting because it demonstrated how an AI model can request that an application execute predefined functions.

For example, I built calculator tools that an AI assistant could use to perform arithmetic operations.

The important distinction is that the model does not execute the Java methods itself. It requests a tool call, and the application executes the corresponding method and returns the result.

Testing AI applications was another important part of the module.

I explored how to test services that use `ChatClient`, how to mock `ChatModel` responses, and how to verify prompt construction without making actual requests to an AI provider.

This was useful because AI applications introduce testing challenges that aren't always present in traditional Spring applications.

Finally, I explored **Model Provider Integration**.

I integrated two providers:

Using Spring profiles, I could switch between providers through configuration without changing the application service implementation.

```
spring:
  ai:
    model:
      chat: ollama
```

Or:

```
spring:
  ai:
    model:
      chat: openai
```

The same application service can interact with either provider through Spring AI's common abstractions.

That was a great way to finish the module because it brought together many of the concepts explored throughout the examples.

The goal wasn't simply to learn how to send a prompt to an AI model.

The bigger goal was to understand how Spring AI helps developers build applications that use language models, structured responses, semantic search, external tools, and different AI providers.

The project is open source, and I'm continuing to build it as I learn:

🔗 GitHub: [https://gith.ub/dsAQrAU5](https://github.com/IamMujuziMoses/spring-by-example-v2)

🔗 GitLab: [https://gitl.ab/gfDEyGH3](https://gitlab.com/IamMujuziMoses/spring-by-example-v2)

There’s still a long way to go, but completing **Module 10 - Spring AI** feels like another important milestone. 🚀

**Next up: Module 11 - GraalVM Native** - exploring native compilation, Spring AOT, runtime hints, native testing, and containerized native applications. Continuing the journey through Spring by Example V2, one focused example at a time.

If you've worked with Spring AI, **which concept do you find most interesting or useful in real-world applications - RAG, tool calling, embeddings, structured output, or model provider integration?**
