Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsBuild a ChatGPT-style Spring Boot app by calling an OpenAI model through Spring AI—not by automating the ChatGPT website. Add Spring AI’s OpenAI starter, provide the API key through an environment variable, and inject a ChatClient to handle requests. The minimal endpoint below returns one answer; a production app also needs authentication, input validation, rate limits, error handling, and a plan for conversation history.
What you are building
Your Spring Boot server receives a request from a browser or other client, calls an OpenAI model through Spring AI, and returns the model’s response. The OpenAI API key stays on the server. This is different from scripting or embedding the consumer ChatGPT website: your application communicates with the model API using API credentials.
A basic request-response flow looks like this:
- A client sends a message to your Spring Boot endpoint.
- The controller passes it to Spring AI’s
ChatClient. - Spring AI sends the request to the configured model provider.
- Your application returns the response to the client.
Spring AI is designed to provide a common model API across providers, with synchronous and streaming interactions. That abstraction can make a provider change easier, but it does not remove provider-specific differences in model availability or behavior.
Choose compatible Spring AI and Spring Boot versions
Check the Spring AI project’s current compatibility guidance before creating the project. Its project page lists Spring AI 2.x with Spring Boot 4.x, and Spring AI 1.1.x with Spring Boot 3.5.x. The OpenAI reference documentation has version labels that do not line up cleanly with that project-page guidance: it is labeled 1.0.9 and points to 2.0.1 as latest stable. Treat the project’s compatibility guidance and the reference for your selected release as authoritative; do not copy a version number from an older example without checking it.
#1 Best Overall
| Spring Boot line | Spring AI line listed by the project page | What to do |
|---|---|---|
| 4.x | 2.x | Use a compatible Spring AI BOM and starter version for the Boot version you select. |
| 3.5.x | 1.1.x | Use a compatible Spring AI BOM and starter version for the Boot version you select. |
These are the version lines listed on the Spring AI project page, not a guarantee that every patch combination is compatible. Pin the Spring AI BOM and starter consistently rather than mixing release lines. The OpenAI reference’s 2.0 snapshot notes an implementation change beginning with 2.0.0-M5, but snapshot documentation is development guidance; rely on the stable reference for your chosen release.
Create the Spring Boot project and add Spring AI
Create a Spring Boot Web application with Spring Initializr, then add the OpenAI model starter. The Maven artifact is org.springframework.ai:spring-ai-starter-model-openai; Gradle uses the same artifact. Select a version compatible with your Spring Boot line and manage it through the matching Spring AI BOM as shown in the documentation for that release.
For Maven, the dependency has this shape; use the compatible version management configured by your project rather than inventing a version here:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
With the starter on the classpath and the API key configured, Spring Boot can auto-configure the OpenAI integration. You can inject the fluent ChatClient.Builder or, for lower-level model access, OpenAiChatModel.
Rank #2
Keep the API key out of source control
Configure Spring AI to read the key from an environment variable instead of placing a secret in a committed properties file:
spring.ai.openai.api-key=${OPENAI_API_KEY}
Set OPENAI_API_KEY in the environment that launches the application. For example, in a Unix-like shell, set it for the current shell before running the app:
export OPENAI_API_KEY="your-key"
./mvnw spring-boot:run
Do not commit the actual key, print it in logs, or send it to browser code. In deployed environments, provide it through that platform’s secret-management mechanism. If a key is exposed, revoke or rotate it through the provider’s account controls and update the deployment’s secret.
Configure the model and request options
Spring AI’s OpenAI configuration supports choosing a model and request options such as temperature through properties. The exact property names and accepted model identifiers can vary by Spring AI release and provider availability, so confirm them in the OpenAI reference for the version you pinned. Do not assume a model name from an old snippet remains available or appropriate for your account.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Rank #3
Keep configuration separate from controller logic. This lets you change model settings without rebuilding the endpoint and makes environment-specific configuration easier to manage.
Add a minimal synchronous chat endpoint
This example uses ChatClient.Builder, builds a client once, and accepts a message as a query parameter. It is deliberately small to show the integration boundary:
import java.util.Map;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
class ChatController {
private final ChatClient chatClient;
ChatController(ChatClient.Builder builder) {
this.chatClient = builder.build();
}
@GetMapping("/ai/generate")
Map<String, String> generate(@RequestParam String message) {
String answer = chatClient.prompt(message).call().content();
return Map.of("generation", answer);
}
}
Start the application with ./mvnw spring-boot:run. Once it is running, a request such as GET /ai/generate?message=Explain%20dependency%20injection returns a JSON object with a generation field. The example is suitable for demonstrating the call path, not for exposing as-is to the public internet.
Return incremental output for a chat interface
A synchronous call waits for a complete response before returning it. For a UI that displays text as it arrives, use Spring AI’s streaming API. The model-level API supports chatModel.stream(prompt); the ChatClient also has a streaming form. Spring AI examples return reactive values such as Flux<ChatResponse>. Confirm the exact method and response shape in the reference for your pinned release.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →Rank #4
A streaming endpoint should be designed together with the client transport. The browser must consume a streaming response format, and your server should propagate disconnects and errors cleanly. Streaming improves how quickly a user can see the beginning of a response; it does not make the model response instantaneous or eliminate the need for timeouts and limits.
Make it a real multi-turn conversation
The minimal endpoint sends only the current message. It does not remember previous turns. For multi-turn chat, give each conversation an identifier and define which prior messages are included in later requests. Spring AI’s tutorial describes carrying previous conversation content forward and demonstrates storing application data in a database.
- Persist messages with a conversation identifier if users need history across requests or sessions.
- Decide how much history to include in a model call; do not assume the model automatically remembers earlier API requests.
- Apply access controls so one user cannot retrieve another user’s conversation.
- Set retention and deletion rules for stored prompts and responses.
Persistence is an application responsibility: the chat endpoint alone does not provide durable conversation history.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Prepare the endpoint for production
Before using the endpoint with real users, add the controls the illustrative controller omits:
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →- Input validation: reject missing, malformed, or excessively large messages.
- Authentication and authorization: restrict who can call the endpoint and access stored conversations.
- Rate limiting: control request volume per user or other appropriate boundary.
- Timeouts and error mapping: handle provider failures and slow responses without exposing credentials or internal details.
- Operational safeguards: avoid logging secrets, and decide how sensitive prompt and response data is handled.
Extend the app when the use case needs it
Use advisors for recurring request patterns
Spring AI advisors provide extension points for recurring behavior around model requests. They can help organize shared concerns as the application grows, rather than repeating the same logic in each controller.
Add retrieval-augmented generation for private documentation
If users need answers grounded in your organization’s documents, add a vector store and a retrieval-augmented generation (RAG) flow. Retrieval can supply relevant private content to a model request; it is a separate capability from simply sending a user’s message to the model.
Use tool calling for application actions
Tool calling lets a model request application-defined functions. Keep authorization and validation in your application: a model’s request to invoke a function is not itself permission to perform a sensitive action.
Connect MCP when the application needs MCP servers
Spring AI’s project guidance includes MCP capabilities for applications that need to consume or expose MCP servers. Add MCP when that integration is part of the use case; it is not required for the basic chat endpoint.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

