AI & data
Chatbot API · Spring Boot + OpenAI
A Spring Boot REST API that connects OpenAI’s GPT-4o-mini to a Java backend, without ever exposing the API key in the browser.
- Individual project
- API keys exposed to the browser
- 0
- endpoints: chat and health
- 2
01Starting point
Context & mission
The problem
Calling an LLM straight from the browser exposes the API key. The right place for that integration is the backend.
The mission
Expose a clean chat endpoint in Java that relays the message to the model and returns a structured reply.
My role
Designed and built on my own.
02Execution
What I did
Chat endpoint
POST /api/chat receives a message and returns the generated reply as structured JSON.
OpenAI integration
The service calls GPT-4o-mini through RestTemplate; the key stays server-side.
Layered code
Separate controller, service, request and response DTOs, and configuration.
Front-end ready
Health-check endpoint and CORS configuration to plug in a web interface.
03Design
Architecture
The controller receives the request, the service calls the OpenAI API, then builds the response DTO returned to the client.
- ClientPOST /api/chat
- ChatController
- ChatServiceRestTemplate
- OpenAIGPT-4o-mini
- ChatResponseJSON DTO
04Impact
Results
- An LLM cleanly integrated into a Java backend.
- An API ready to serve any front end.
05Wrap-up
Takeaways
What this project provesThat I integrate an LLM in the right place: server-side, behind a clear REST contract.
06Technology
Full stack
- Java 17
- Spring Boot 3.4
- Spring Web
- RestTemplate
- Maven
- OpenAI API
- GPT-4o-mini