A step-by-step guide to integrating Google's Gemini models into a Spring Boot application using Spring AI's `spring-ai-starter-model-google-genai` module. Covers Maven setup with Spring AI BOM 2.0.0, explicit chat provider activation via `spring.ai.model.chat=google-genai`, and two abstraction layers: the low-level `GoogleGenAiChatModel` and the fluent `ChatClient` API. Also demonstrates prompt templates for dynamic inputs, live Google Search grounding to reduce hallucinations, reactive token streaming with WebFlux `Flux<String>`, and MockMvc-based unit testing that avoids real API calls.

9m read timeFrom feeds.feedblitz.com
Post cover image
Table of contents
1. Introduction2. Project Setup3. Core Text and Content Generation Strategies4. Testing5. Conclusion

Questions this post answers

How do I activate the Google GenAI chat provider in Spring AI without it auto-configuring from the classpath?

You must explicitly set `spring.ai.model.chat=google-genai` in your `application.properties`. In recent Spring AI versions, adding a starter to the classpath no longer auto-activates it, preventing conflicts when multiple LLM providers are present. You also need `spring.ai.google.genai.chat.model=gemini-2.5-flash` and `spring.ai.google.genai.api-key` pointing to your Google AI Studio key. Java developers wiring up multiple LLM providers track Spring AI configuration changes like this on daily.dev.

How do I enable live Google Search grounding in Spring AI with the Google GenAI module?

Set `spring.ai.google.genai.chat.google-search-retrieval=true` in your `application.properties`. With this flag enabled, queries about current events are automatically grounded using live Google Search results, reducing hallucinations caused by the model's training cutoff. No code changes to the `ChatClient` call chain are required — the grounding is applied globally at the configuration level. Developers building real-time Q&A features on Gemini find the latest Spring AI grounding updates on daily.dev.

How do I stream Gemini responses token by token in a Spring Boot application using Spring AI?

Return a `Flux<String>` from your service by calling `.stream().content()` on the `ChatClient` prompt chain. In the controller, annotate the endpoint with `produces = MediaType.TEXT_EVENT_STREAM_VALUE` to deliver tokens as Server-Sent Events. Spring Boot's WebFlux integration handles the non-blocking pipeline, sending each token chunk to the client as it arrives rather than waiting for the full response. Teams reducing perceived latency in AI chat UIs follow Spring AI streaming patterns on daily.dev.

2.1K Impressions