How I Built a Custom AI Agent Using Java Spring Boot and Vertex AI REST API

Building a Production-Ready Gemini AI Chatbot Using Spring Boot 2.6 and JDK 8

If you're a Java developer aiming to integrate cutting-edge AI into your enterprise applications, this comprehensive blog will guide you through the process of building a fully functional AI chatbot using Spring Boot 2.6, JDK 8, and Gemini Vertex AI through REST APIs. The chatbot supports real-time file uploads, contextual conversations, intelligent document summarization, and dynamic response generation. It is especially useful for NBFCs, legal departments, banks, and enterprises undergoing digital transformation.

Project Overview

This chatbot, built using the Gemini Vertex AI model, functions as a document-aware virtual assistant that enables users to:

  • Upload files in formats like PDF, DOCX, or TXT and ask questions about the content.
  • Receive contextual responses from a Gemini-powered custom agent.
  • Download AI responses as PDF, Word, or plain text files instantly.
  • Handle file uploads dynamically — inline (for files under 7MB) and cloud-based (for larger files).
  • Display responses using the Summer Note editor for a user-friendly interface.

All of this is built upon a clean, modular backend architecture powered by Java and Spring Boot and securely integrated with Google Cloud AI services.

Technology Stack

  • Spring Boot 2.6: Manages REST controllers, service layers, and dependency injection.
  • JDK 8: Ensures compatibility with legacy infrastructure.
  • Gemini Vertex AI: Offers generative AI capabilities accessed through REST APIs.
  • application.yml: Central configuration hub including proxy settings, API routes, model IDs, and file handling logic.
  • GoogleCredentials: Loaded using GoogleCredentials.fromStream() with enterprise proxy support.
  • Summer Note: Enables rich-text chat output formatting and dynamic editing.

Key REST APIs Developed

The project includes several well-structured RESTful services:

REST Integration: Gemini + Cloud Upload via a Single API

Instead of exposing multiple endpoints, the entire flow is handled via a single internal API endpoint:

  • POST /bancsai/api/doc/processJson – Main REST entrypoint that accepts:
    • Just a prompt (text-only)
    • Prompt + file (less than 7MB): converted to inlineData
    • Prompt + large file (more than 7MB): uploaded to GCS, returning fileUri

This backend service internally integrates with two Gemini Vertex AI REST API endpoints:

  • model-endpoint:
    https://{location}-aiplatform.googleapis.com/v1/projects/{projectId}/locations/{location}/publishers/google/models/{modelId}:generateContent
    Used to send the prompt and file reference to Gemini for content generation.
  • gcs-upload-url:
    https://storage.googleapis.com/upload/storage/v1/b/{bucketName}/o?uploadType=media&name={filename}
    Used to upload large files and get back a usable fileUri.

Sample JSON: Request to Vertex AI (Text Prompt or File)

{
  "contents": [
    {
      "parts": [
        {
          "text": "Summarize this document."
        },
        {
          "fileData": {
            "mimeType": "application/pdf",
            "fileUri": "https://storage.googleapis.com/geminifile-docs/sample.pdf"
          }
        }
      ]
    }
  ]
}

Sample JSON: Response from Gemini Model

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "Here's a summary of the uploaded document: ..."
          }
        ]
      },
      "finishReason": "STOP"
    }
  ]
}

This structure allows your chatbot to support flexible document-based AI processing using a unified API approach that works across both small and large files.

Each API is mapped via Spring Boot controllers and services like DocumentController, GeminiService, and FileStorageService.

application.yml Configuration Highlights

The application.yml file configures everything from Gemini access credentials to cloud bucket paths and proxy handling. Below is a sample:

gemini:
  location: us-central1
  project-id: geminifile
  model-id: gemini-2.0-flash
  bucket-name: geminifile-docs

  template:
    model-endpoint: "https://{location}-aiplatform.googleapis.com/v1/projects/{projectId}/locations/{location}/publishers/google/models/{modelId}:generateContent"
    gcs-upload-url: "https://storage.googleapis.com/upload/storage/v1/b/{bucketName}/o?uploadType=media&name={filename}"

  access-token: ${GEMINI_ACCESS_TOKEN:}
  instruction-text: >
    Respond based on the user’s intent: summarize, extract data, answer questions precisely...

Challenges Solved

  • Proxy Errors: Solved issues with GoogleCredentials.fromStream() behind firewalls using custom proxy config in application.yml.
  • File Size Limits: Implemented conditional routing to handle inline vs. cloud uploads depending on size.
  • Data Format Handling: Auto-switched between inlineData and fileUri formats for Gemini's API payloads.
  • Custom Summary Instructions: Developed `/summary` endpoint to handle specific token-based summarization logic.
  • Dynamic Downloads: Added AI Response converters for PDF/Word generation with formatting support.
  • Frontend Optimization: Used Summer Note for editable, styled chat interfaces with export tools.

Frontend Features

  • Live AI chat rendered in Summer Note.
  • One-click PDF and Word downloads for every message.
  • Option to clear conversation history or switch response formats.
AI Chatbot Interface using Java Spring Boot and Gemini Vertex AI with file upload and PDF download

What’s Coming Next

In the next article, I will break down the full application.yml file, explain how to wire it to REST APIs and services, and demonstrate how to consume these endpoints from a React frontend or Postman.

Subscribe for more posts about Java + Gemini + AI API Development.


Want full source code? We’ll publish it on GitHub soon with a YouTube walkthrough. Follow us at CodeNib.


  • Unique developer content, no AI copy-paste
  • Structured with heading levels, lists, and preformatted code
  • Mobile-optimized with light CSS and no emoji clutter
  • Keyword-rich: Spring Boot Gemini, Java AI REST API, Document Chatbot

Author: Team CodeNib | Project: Gemini Custom Agent using Java

Post a Comment

0 Comments