api-spring-ai-demo-gemini-v1

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.

View source on GitHub Portfolio index

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

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

👉 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