Demo project built with Spring Boot 4 + Spring AI 2.0 that integrates Google's Gemini model (via spring-ai-starter-model-google-genai) to expose a chat service through a REST API.
Description
The application starts a web server (port 8087) with an endpoint that receives a question from the user and forwards it to the configured Gemini model, returning the AI-generated response. It also includes a CommandLineRunner that runs a sample query against Gemini on startup and prints it to the console, as a quick sanity check of the integration.
Technologies
- Java 21
- Spring Boot 4.1.0
- Spring AI 2.0.0 (
spring-ai-starter-model-google-genai) - Maven (with the
mvnwwrapper) - Model:
gemini-3.5-flash
Project structure
src/main/java/com/mx/development/jose/said/
├── ApiSpringAiDemoV1Application.java # Main class (Spring Boot entry point)
├── controller/
│ └── ChatController.java # Exposes the chat REST endpoint
├── service/
│ ├── ChatService.java # Chat service contract
│ └── ChatServiceImpl.java # Implementation using Spring AI's ChatClient
└── runner/
└── ChatRunner.java # Runs a sample query on application startup
src/main/resources/
├── application.yml # General configuration (port, active profile, logging)
└── application-gemini.yml # Gemini provider-specific configuration (API key, model)
Prerequisites
- JDK 21 or higher
- A Google Gemini API key, generated at:
👉 https://aistudio.google.com/projects
Configuration
The project uses the gemini profile, active by default in application.yml. The API key is read from the API_KEY environment variable (see application-gemini.yml):
spring:
ai:
google:
genai:
api-key: ${API_KEY}
chat:
options:
model: gemini-3.5-flash
Before running the application, set the environment variable with your API key:
export API_KEY=your_gemini_api_key
Running the application
Using the Maven Wrapper included in the project:
./mvnw spring-boot:run
On Windows:
mvnw.cmd spring-boot:run
The application will be available at http://localhost:8087.
On startup, you'll see the ChatRunner's response to the sample question "¿Qué es Spring AI?" printed to the console.
API usage
Chat endpoint
GET /api/v1/chat-service/chat?question={question}
Example:
curl "http://localhost:8087/api/v1/chat-service/chat?question=%C2%BFQu%C3%A9%20es%20Spring%20AI%3F"
Response: plain text with the response generated by the Gemini model.
Notes
- Debug logging for Spring AI and the web client is enabled (
DEBUG) inapplication.yml, which is useful for inspecting requests/responses to the model. - The commented-out block in
ChatRunner.javashows an example of using a system prompt together with the user message to customize the assistant's behavior (e.g., forcing responses in Spanish as a Spring software architect).