Artificial Intelligence is now a practical tool for developers, not just a complex research topic. As a Java developer, you can easily integrate AI into your backend using simple API calls.
In this guide, you will learn how to build your first AI-powered API using Spring Boot and OpenAI, in a clean and practical way.

๐ Understanding the Approach
We are building a simple REST API that:
- Accepts a prompt
- Sends it to an AI service
- Returns the response
Flow:
User โ Spring Boot API โ OpenAI API โ Response โ User
โ๏ธ Project Setup
Create a Spring Boot project with:
- Java 11+
- Spring Web dependency
- Maven
๐ฆ Maven Dependency
org.springframework.boot
spring-boot-starter-web
๐งฉ Request & Response Classes
package com.kscodes.ai;
public class AIRequest {
private String prompt;
//Getters and Setters
}
package com.kscodes.ai;
public class AIResponse {
private String response;
public AIResponse(String response) {
this.response = response;
}
public String getResponse() {
return response;
}
}
๐ง AI Service (Core Logic)
package com.kscodes.ai;
import org.springframework.stereotype.Service;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
@Service
public class AIService {
private static final String API_URL = "https://api.openai.com/v1/chat/completions";
private static final String API_KEY = System.getenv("OPENAI_API_KEY");
public String generateResponse(String prompt) {
try {
String requestBody = "
{
"model": "gpt-4o-mini",
"messages": [
{
"role": "user",
"content": "%s"
}
],
"temperature": 0.7
}
".formatted(prompt);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(API_URL))
.header("Authorization", "Bearer " + API_KEY)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
.build();
HttpClient client = HttpClient.newHttpClient();
HttpResponse response =
client.send(request, HttpResponse.BodyHandlers.ofString());
return response.body();
} catch (Exception e) {
e.printStackTrace();
return "Error while calling AI service";
}
}
}
๐ REST Controller
package com.kscodes.ai;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/ai")
public class AIController {
@Autowired
private AIService aiService;
@PostMapping("/generate")
public AIResponse generate(@RequestBody AIRequest request) {
String aiResult = aiService.generateResponse(request.getPrompt());
return new AIResponse(aiResult);
}
}
โถ๏ธ Testing the API
Endpoint:
POST http://localhost:8080/ai/generate
Request Body:
{
"prompt": "Explain Spring Boot in simple terms"
}
๐ Important Best Practice
Never hardcode your API key:
export OPENAI_API_KEY=your_key_here
๐ก Real-World Use Cases
You can extend this API to build:
- Chatbots
- Document summarization
- Email generation
- Customer support automation
For example:
๐ Insurance apps can summarize policy documents instantly.
๐ฏ Why This Matters
This pattern is the foundation of AI integration in backend systems.
Once you understand this:
- You can plug AI into any microservice
- You can scale AI features easily
- You can build enterprise-grade AI solutions
๐ Summary
- Built a Spring Boot AI API
- Integrated OpenAI using HttpClient
- Created clean Controller + Service structure
- Followed secure API practices
๐ฌ Final Thought
AI is just another service in your architecture.
Once you know how to call it properly,
you can build powerful, intelligent applications with ease.