The rise of Generative AI has opened up incredible possibilities for developers. Integrating ChatGPT (OpenAI) into your Spring Boot application allows you to build intelligent features like automated customer support, content generation, and smart data analysis.
In this guide, we’ll walk through the process of connecting your Spring Boot application to the OpenAI API using the modern RestClient (introduced in Spring 6) to build a functional AI-powered service.
Prerequisites
Before we start coding, make sure you have:
- A Spring Boot project (Java 17 or higher recommended).
- An OpenAI API Key (you can get one from the OpenAI Dashboard).
1. Setting Up Dependencies
We’ll use the standard Spring Boot Web starter. If you’re using Spring Boot 3.2+, we can leverage the new RestClient.
Maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2. Configuring the API Key
Store your OpenAI API key in src/main/resources/application.properties (or use environment variables for better security).
openai.api.key=your_api_key_here
openai.api.url=https://api.openai.com/v1/chat/completions
3. Creating the Request and Response Models
OpenAI’s Chat Completion API expects a specific JSON structure. Let’s create the necessary DTOs (Data Transfer Objects).
ChatRequest.java
public record ChatRequest(
String model,
List<Message> messages
) {
public record Message(String role, String content) {}
}
ChatResponse.java
public record ChatResponse(
List<Choice> choices
) {
public record Choice(Message message) {}
public record Message(String role, String content) {}
}
4. Building the AI Service
Now, let’s create a service that communicates with OpenAI. We’ll use RestClient for a clean, fluent API experience.
@Service
public class ChatGptService {
private final RestClient restClient;
@Value("${openai.api.key}")
private String apiKey;
@Value("${openai.api.url}")
private String apiUrl;
public ChatGptService(RestClient.Builder builder) {
this.restClient = builder.build();
}
public String getChatResponse(String prompt) {
ChatRequest request = new ChatRequest(
"gpt-3.5-turbo",
List.of(new ChatRequest.Message("user", prompt))
);
ChatResponse response = restClient.post()
.uri(apiUrl)
.header("Authorization", "Bearer " + apiKey)
.body(request)
.retrieve()
.body(ChatResponse.class);
return response.choices().get(0).message().content();
}
}
5. Creating the Controller
Finally, expose an endpoint to interact with our service.
@RestController
@RequestMapping("/api/ai")
public class AIController {
private final ChatGptService chatGptService;
public AIController(ChatGptService chatGptService) {
this.chatGptService = chatGptService;
}
@GetMapping("/chat")
public String chat(@RequestParam String prompt) {
return chatGptService.getChatResponse(prompt);
}
}
6. Testing the Integration
Run your Spring Boot application and send a GET request:
curl "http://localhost:8080/api/ai/chat?prompt=Tell+me+a+joke+about+Java"
The AI should respond with a generated joke!
Best Practices for AI Integration
- Error Handling: OpenAI API might rate-limit you or experience downtime. Use
@RetryableorCircuitBreaker(Resilience4j). - Streaming: For long responses, consider using
WebClientto support Server-Sent Events (SSE). - Prompt Engineering: Be specific in your system messages to get better quality responses.
- Security: Never commit your API keys to version control. Use a Secrets Manager or Environment Variables.
Conclusion
Integrating ChatGPT into Spring Boot is straightforward thanks to Spring’s modern networking tools. Whether you’re building a chatbot or enhancing your data processing, the combination of Java’s robustness and OpenAI’s intelligence is a winning formula.
Member discussion
0 commentsStart the conversation
Become a member of >hacksubset_ to start commenting.
Already a member? Sign in