Build AI-Powered Java App: Integrating ChatGPT with Spring Boot
Updated: Apr 30, 2025
Most developers worldwide use Spring Boot to develop REST APIs due to its flexibility, ease of use, and scalability. It helps in simplifying the setup configurations required for spring-based apps. Meanwhile, artificial intelligence is no longer a trending concept; it is becoming essential to modern backend development. Whether it's having chatbots or personal assistance, the demand for smart and conversational systems is on the rise.
In this blog, we will walk through how to integrate ChatGPT with a Spring Boot application. Spring Boot is a go-to framework for developers who want to build production-ready backend systems in Java. On the other hand, the ChatGPT API gives us the ability to produce a human-like response from the prompt provided. Bringing them together means we will be able to build AI-powered microservices to our existing Java projects, or we will be able to enable dynamic content creation in real time, at the same time, all this by maintaining full control of our backend logic, scaling, and security. Let's build this together step by step!
Create a Spring Boot Project
We need to create and set up a Spring Boot Project in any IDE.
Start with Spring initializer and choose Maven or Gradle.
Add the required Spring web dependencies under dependencies.
Generate the project and open it in any IDE.

After downloading the generated Spring Boot project, extract all the files and import them into your preferred IDE (I am using Eclipse IDE for this project, but feel free to use your preferred one).
Generate OpenAI API Key
Once the Spring Boot app is imported, the next step is to obtain the OpenAI Key, Model, and Endpoint URL.
Let’s have a look at some of the mandatory API request and response parameters before creating the OpenAI key.
Model: The language model we will be using to send a request to, there are a few versions available, here I am using GPT-4.1
Message: They are the prompts to the model., It contains a sequence of interactions between the user and the assistant. Each message will include:
Role: Specifies whether it is the” user” or the “assistant” sending the message. It can be a part of a request or a response.
Content: The actual text of the message

You can view a sample request under the chat completion section of the OpenAPI AI documentation. Copy the endpoint URL and model from this section.
Now, to obtain the API Key, we will need to log in to the OpenAI Platform. Once logged in,
Go to the API keys under settings
Generate a new key and save it somewhere, as you won't be able to access it again
We will be setting this key as the authorization header whenever calling the API.
Configuring the application.properties
Before setting up the project structure, we need to configure a few important properties from the API documentation we have copied in the above step. This includes the API key created from the OpenAPI platform, the model name, and the endpoint URL.

Setting up the Project Structure
After configuring application.properties, it's time for us to set up the code structure on Eclipse (or any IDE). We will be separating different responsibilities into different packages according to spring Boot framework.
Controller Package
This package will contain the rest controller class for handling the HTTP requests. It will have an API endpoint where we can send the prompt, and the backend communicates with the OpenAI API to fetch the response.
I have created a ChatGPT controller to expose an endpoint so that my Postman can trigger the AI response. Here is the code to do the same.
@RestController
@RequestMapping("/api/chat")
public class ChatGPTController {
private final ChatGPTService chatGPTService;
public ChatGPTController(ChatGPTService chatGPTService) {
this.chatGPTService = chatGPTService;}
@PostMapping
public String chat(@RequestBody PromptRequest promptRequest) {
return chatGPTService.getChatResponse(promptRequest);}}
Config Package
This Package is created to store the configuration classes required for setting up connections, headers, or any other properties related to OpenAI. In OpenAIConfiguration class, we are injecting the OpenAI base URL from application.properties. Here is the snippet of the code for OpenAPIConfiguration.java
@Configuration
public class OpenAPIConfiguration {
@Value("${openapi.api.url}")
private String apiUrl;
@Bean
public RestClient restclient() {
return RestClient.builder().baseUrl(apiUrl).build();
}}
DTO(Data Transfer Object) Package
This package is used for structuring the request and response objects. Open AI expects a specific JSON format, so we will create DTO classes for Request, Response, and Prompt Response. I used Java’s record feature to create immutable DTOs for better performance. Here is a snippet of the record I created for ChatGPTRequest
public record chatGPTRequest(String model, List<Message> messages) {
public static record Message(String role, String content) {
}}
To complete the DTO package, create record for ChatGPTResponse and the prompt class in a similar way.
Service Package
This package is used for writing the business logic, which connects the controller to the OpenAI API. The key responsibilities are to accept the user’s input as a prompt, build a request, and send the request using Spring’s new RestClient. Also, extract the response from OpenAI and return the generated test. Here is a snippet of the ChatGPT Service class under this package:
public String getChatResponse(PromptRequest promptRequest) {
chatGPTRequest cGptRequest = new chatGPTRequest(model,
List.of(new chatGPTRequest.Message("user",promptRequest.prompt())));
ChatGPTResponse response = restClient.post().uri(openAiUrl)
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json").body(cGptRequest).retrieve().body(ChatGPTResponse.class);
return response.choices().get(0).message().content();}
Finally, we have a project structure as shown in the image below:

Testing the integration with Postman
Now that we have set up the Spring Boot application, it's time to test it on Postman.
We will do it step-by-step
Start the application on the IDE.
Open Postman and create a POST request:
Method: POST
Headers: Content-Type: application/json
Body: {
"prompt": "What is Spring Boot?"
}
Send the request.
If everything is set up correctly, you should be able to get the expected response for the prompt. The response will look like the image below:

And finally, we have successfully integrated OpenAPI’s ChaTGPT API into a Spring Boot Application and verified its working on Postman
What's next
Integrating OpenAI’s ChatGPT with Spring Boot opens up new possibilities for enhancing user interactions with AI. This will allow our existing /new application to generate smart responses ideal for chatbots and virtual assistance. We can also improve the user experience by streaming the response from ChatGPT in real time. Also, a frontend can be built using React or AngularJS to turn this into a full web-based chatbot.
If you are curious to explore the complete code for this integration or want to experiment yourself, feel free to visit my GitHub repository:
Happy Coding!


