Over the last few articles, we've learned the fundamentals of Spring AI using different concepts like prompts, memory, RAG, tools, and agents. In this article, we'll focus on something much simpler—connecting a Spring Boot application to Google Vertex AI and chatting with Gemini.
If you've already used the OpenAI integration, you'll notice that the application code is almost identical. The biggest difference is authentication, and that's exactly what we'll cover.
Why Vertex AI?
Google provides two ways to access Gemini models.
- Google AI Studio - Great for learning and quick experiments using an API key.
- Google Vertex AI - Enterprise platform for production workloads.
Vertex AI offers:
- Enterprise authentication using Google Cloud IAM
- Security and access control
- Monitoring and logging
- Quotas and billing management
- Integration with other Google Cloud services
For production applications, Vertex AI is the recommended choice.
Local Vertex AI Setup
Unlike OpenAI, Vertex AI doesn't normally use an API key. Instead, it authenticates using Application Default Credentials (ADC). Spring AI automatically discovers these credentials and uses them whenever it communicates with Vertex AI.
1. Install Google Cloud CLI
Download and install the Google Cloud CLI.
https://cloud.google.com/sdk
Verify the installation.
gcloud version2. Login
gcloud auth login
Your browser opens and asks you to sign in with your Google account.
3. Select your project
gcloud config set project YOUR_PROJECT_ID
Example
gcloud config set project spring-ai-demo
Verify it.
gcloud config list4. Create Application Default Credentials
This is the important step.
gcloud auth application-default login
Google stores your credentials locally. Spring AI automatically discovers these credentials and obtains access tokens whenever it calls Vertex AI. You don't need to write any authentication code.
5. Verify everything
gcloud auth application-default print-access-token
If you receive an access token, your machine is correctly configured. You're now ready to build the application.
Create the Spring Boot Project
Generate a standard Spring Boot project with:
- Spring Web
Then add the Spring AI dependency org.springframework.ai:spring-ai-starter-model-google-genai.
application.yml
spring:
ai:
google:
genai:
project-id: your-project-id
location: us-central1
chat:
model: gemini-2.5-flash
temperature: 0.7
Notice something interesting. There is no API key. Spring AI automatically uses the Application Default Credentials that you created earlier.
Chat Service
package com.slmanju.demo;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.stereotype.Service;
@Service
public class GeminiService {
private final ChatClient chatClient;
public GeminiService(ChatClient.Builder builder) {
this.chatClient = builder.build();
}
public String chat(String message) {
return chatClient.prompt(message)
.call()
.content();
}
}
As you can see, the code is exactly the same as our previous OpenAI examples. Changing AI providers usually requires changing only the dependency and configuration.
REST Controller
package com.slmanju.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/chat")
public class ChatController {
private final GeminiService service;
public ChatController(GeminiService service) {
this.service = service;
}
@GetMapping
public String chat(@RequestParam String message) {
return service.chat(message);
}
}Run the Application
Start the Spring Boot application and call:
GET http://localhost:8080/chat?message=Explain dependency injection in Spring.
Or using curl:
curl "http://localhost:8080/chat?message=Explain%20dependency%20injection%20in%20Spring"
You should receive a response generated by Gemini.
Summary
Connecting Spring AI to Google Vertex AI is surprisingly straightforward.
The application code is almost identical to the OpenAI integration. The main difference is authentication, where Vertex AI uses Application Default Credentials instead of an API key.
Once ADC is configured, Spring AI automatically authenticates with Vertex AI, allowing you to focus entirely on building your AI application.

Comments
Post a Comment