Generative AI is transforming how applications interact with users—enabling natural conversations, intelligent responses, and dynamic content generation. With Spring Boot as a backend framework and Google Generative AI (Gemini models) as the intelligence layer, you can build a simple chat API that lets clients send messages and get AI-powered responses.
In this article, we’ll walk through how to set up a REST API in Spring Boot that connects to Google’s Gemini API for conversational AI.
Why Spring Boot + Google GenAI?
- Spring Boot makes it easy to set up REST APIs quickly with minimal configuration.
- Google Generative AI (Gemini) provides state-of-the-art models for natural conversations, text generation, and reasoning.
- Together, they give you a lightweight but powerful way to integrate AI into your apps.
Prerequisites
Before starting, make sure you have:
- Java 17+ installed
- Spring Boot 3.x
- A Google Cloud project with the Generative AI API enabled
- A Gemini API Key (exported as an environment variable)
export GEMINI_API_KEY="your-google-genai-api-key"
Step 1: Create a Spring Boot Project
Generate a project using Spring Initializr:
- Project: Maven
- Dependencies: Spring Web and Lombok (optional for less boilerplate)
Step 2: Add Dependency for Google GenAI Client
In your pom.xml
, include:
<dependency>
<groupId>com.google.genai</groupId>
<artifactId>genai</artifactId>
<version>0.1.0</version> <!-- check latest available -->
</dependency>
This gives access to the Google Generative AI SDK.
Step 3: Application Properties
Configure your API key inside application.properties
:
spring.application.name=chat-api
gemini.api.key=${GEMINI_API_KEY}
Step 4: Create a Chat Request DTO
package com.example.chatapi;
import lombok.Data;
@Data
public class ChatRequest {
private String message;
}
Step 5: Implement the Chat Service
package com.example.chatapi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.google.genai.Client;
import com.google.genai.types.GenerateContentResponse;
@Service
public class ChatService {
private final Client client;
public ChatService(@Value("${gemini.api.key}") String apiKey) {
this.client = new Client(apiKey); // initialize with API key
}
public String askAI(String userMessage) {
GenerateContentResponse response = client.models.generateContent(
"gemini-2.5-flash", // choose Gemini model
userMessage,
null
);
return response.text();
}
}
Step 6: Expose REST API Endpoint
package com.example.chatapi;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/chat")
@RequiredArgsConstructor
@CrossOrigin(origins = "*") // You can add your frontend url here
public class ChatController {
private final ChatService chatService;
@PostMapping("/ask")
public ResponseEntity<String> ask(@RequestBody ChatRequest chatRequest) {
String reply = chatService.askAI(chatRequest.getMessage());
return ResponseEntity.ok(reply);
}
}
Step 7: Test the Chat API
Start your application:
mvn spring-boot:run
Send a request with curl
or Postman:
curl -X POST http://localhost:8080/api/chat/ask \
-H "Content-Type: application/json" \
-d '{"message": "Hello, can you explain what Spring Boot is?"}'
✅ You should receive a response generated by the Gemini model.
Conclusion
By combining Spring Boot with Google’s Generative AI, you can quickly build a REST API that powers intelligent chat experiences. This foundation can be expanded into customer support bots, productivity assistants, or interactive educational tools—all backed by Gemini’s state-of-the-art AI capabilities.
Top comments (0)