{"slug": "building-spring-by-example-v2-completing-module-10-spring-ai", "title": "Building Spring by Example V2: Completing Module 10 - Spring AI", "summary": "A developer completed Module 10 of the spring-by-example project, a collection of small, focused examples documenting Spring concepts, covering Spring AI. The module walks through ChatClient and ChatModel usage, externalized prompt templates, structured output via BeanOutputConverter, embeddings and vector stores, retrieval-augmented generation, tool calling with application-executed functions, and testing by mocking ChatModel responses. It also covers integrating multiple model providers and switching between them using Spring profiles.", "body_md": "When I started learning Spring, I noticed something about many of the tutorials I found.\n\nThey often showed how to use a feature, but not always why it worked or when you should choose one approach over another.\n\nI learn best by building things and documenting what I discover, so I started a project called **spring-by-example**.\n\nThe 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.\n\nI’m happy to say that I’ve now completed **Module 10 - Spring AI**. 🎉\n\nThis 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.\n\nThe module covers:\n\nOne 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.\n\nI 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`.\n\nFrom there, I explored prompt templates.\n\nInstead of hardcoding prompts directly into application logic, I learned how to externalize them and provide dynamic values using placeholders.\n\nFor example:\n\n```\nPromptTemplate promptTemplate = new PromptTemplate(\n        \"Explain {technology} to {audience}.\"\n);\n\nPrompt prompt = promptTemplate.create(Map.of(\n        \"technology\", \"Spring Boot\",\n        \"audience\", \"Java developers\"\n));\n```\n\nStructured output introduced another interesting concept.\n\nRather than working with unstructured text responses, I explored how Spring AI can convert model responses into Java objects using `BeanOutputConverter`.\n\nThis makes it easier to work with predictable response structures in application code.\n\nEmbeddings and vector stores were another important part of the module.\n\nI explored how text can be converted into numerical vector representations that capture semantic meaning.\n\nThis led to vector stores, where I learned how to store documents and retrieve relevant information through similarity searches.\n\nOne of the most interesting examples was **Retrieval-Augmented Generation (RAG)**.\n\nRAG combines information retrieval with language model generation.\n\nInstead of relying entirely on the model's existing knowledge, an application can retrieve relevant documents and provide them as context when generating an answer.\n\nThe simplified flow looks like this:\n\n```\nUser Question\n      |\n      v\nVector Similarity Search\n      |\n      v\nRetrieve Relevant Documents\n      |\n      v\nBuild Prompt with Context\n      |\n      v\nLanguage Model\n      |\n      v\nGenerated Answer\n```\n\nThis helped me understand how AI applications can provide answers grounded in specific information sources.\n\nI also explored **Tool Calling**.\n\nThis was particularly interesting because it demonstrated how an AI model can request that an application execute predefined functions.\n\nFor example, I built calculator tools that an AI assistant could use to perform arithmetic operations.\n\nThe 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.\n\nTesting AI applications was another important part of the module.\n\nI 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.\n\nThis was useful because AI applications introduce testing challenges that aren't always present in traditional Spring applications.\n\nFinally, I explored **Model Provider Integration**.\n\nI integrated two providers:\n\nUsing Spring profiles, I could switch between providers through configuration without changing the application service implementation.\n\n```\nspring:\n  ai:\n    model:\n      chat: ollama\n```\n\nOr:\n\n```\nspring:\n  ai:\n    model:\n      chat: openai\n```\n\nThe same application service can interact with either provider through Spring AI's common abstractions.\n\nThat was a great way to finish the module because it brought together many of the concepts explored throughout the examples.\n\nThe goal wasn't simply to learn how to send a prompt to an AI model.\n\nThe 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.\n\nThe project is open source, and I'm continuing to build it as I learn:\n\n🔗 GitHub: [https://gith.ub/dsAQrAU5](https://github.com/IamMujuziMoses/spring-by-example-v2)\n\n🔗 GitLab: [https://gitl.ab/gfDEyGH3](https://gitlab.com/IamMujuziMoses/spring-by-example-v2)\n\nThere’s still a long way to go, but completing **Module 10 - Spring AI** feels like another important milestone. 🚀\n\n**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.\n\nIf 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?**", "url": "https://wpnews.pro/news/building-spring-by-example-v2-completing-module-10-spring-ai", "canonical_source": "https://dev.to/iammujuzimoses/building-spring-by-example-v2-completing-module-10-spring-ai-42a7", "published_at": "2026-11-26 08:07:51+00:00", "updated_at": "2026-11-26 08:13:41.584679+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "ai-tools", "developer-tools"], "entities": ["Spring", "Spring AI", "Spring Boot", "ChatClient", "ChatModel", "BeanOutputConverter", "PromptTemplate"], "also_reported_by": [], "takeaway": "Spring AI wraps provider calls behind ChatClient/ChatModel, lets prompts be externalized with PromptTemplate placeholders, converts responses to Java objects with BeanOutputConverter, and allows ChatModel to be mocked so prompt construction can be tested without real provider requests.", "recommendation": {"advice": "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.", "playbook": "evals-testing", "playbook_url": "https://wpnews.pro/playbooks/evals-testing"}, "alternates": {"html": "https://wpnews.pro/news/building-spring-by-example-v2-completing-module-10-spring-ai", "markdown": "https://wpnews.pro/news/building-spring-by-example-v2-completing-module-10-spring-ai.md", "text": "https://wpnews.pro/news/building-spring-by-example-v2-completing-module-10-spring-ai.txt", "jsonld": "https://wpnews.pro/news/building-spring-by-example-v2-completing-module-10-spring-ai.jsonld"}}