July 1, 2026

Spring AI Tutorial — Your First REST Endpoint with OpenAI (2026)

Updated — July 1, 2026 · Cornerstone Spring AI tutorial — first REST endpoint with OpenAI.

Kindson Munonye · Software engineer & technical author
GitHub · LinkedIn · About · YouTube
Last updated by Kindson Munonye — July 1, 2026


📚 Tutorial hubs:
AI Developer Tutorials ·
Spring Boot ·
Angular ·
CRUD + REST guide

Source code: munonye-ai-chat-spring-angular on GitHub

Estimated reading time: 12–15 minutes · Last updated: July 1, 2026


This Spring AI tutorial step by step walks you through your first LLM-powered REST endpoint on Spring Boot 3. You will call OpenAI from Java using ChatClient, keep API keys out of source code, and verify with curl — the backend foundation for our AI Developer Tutorials series.

If you have built REST APIs with our Angular CRUD + Spring Boot series, you already know the architecture; we are adding an AI layer on top.

Prerequisites: Java 17+, Maven, OpenAI API key, basic Spring Boot REST knowledge.
Time: ~45 minutes.

Architecture

Client (curl / Angular)
    POST /api/chat  { "message": "Hello" }
         ▼
ChatController → ChatClient → OpenAI API
         ▼
    { "reply": "..." }

Step 1 — Create the project

curl https://start.spring.io/starter.zip \
  -d dependencies=web \
  -d javaVersion=17 \
  -d name=ai-chat-api \
  -o ai-chat-api.zip
unzip ai-chat-api.zip && cd ai-chat-api

Add Spring AI BOM and OpenAI starter in pom.xml:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.ai</groupId>
      <artifactId>spring-ai-bom</artifactId>
      <version>1.0.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
  </dependency>
</dependencies>

Step 2 — Configure OpenAI

# application.properties
server.port=8080
spring.ai.openai.api-key=${OPENAI_API_KEY}
spring.ai.openai.chat.options.model=gpt-4o-mini
spring.ai.openai.chat.options.temperature=0.7

Run with:

export OPENAI_API_KEY=sk-...
./mvnw spring-boot:run

Never commit API keys. Use environment variables or a secrets manager in production (see M11-A secure AI tutorial).

Step 3 — Chat REST endpoint

@RestController
@RequestMapping("/api/chat")
@CrossOrigin(origins = "http://localhost:4200")
public class ChatController {

  private final ChatClient chatClient;

  public ChatController(ChatClient.Builder builder) {
    this.chatClient = builder
        .defaultSystem("You are a helpful Java and Spring Boot assistant.")
        .build();
  }

  @PostMapping
  public ChatResponse chat(@RequestBody ChatRequest request) {
    String reply = chatClient.prompt()
        .user(request.message())
        .call()
        .content();
    return new ChatResponse(reply);
  }
}

public record ChatRequest(String message) {}
public record ChatResponse(String reply) {}

Step 4 — Test with curl

curl -s -X POST http://localhost:8080/api/chat \
  -H "Content-Type: application/json" \
  -d '{"message":"Explain Spring Boot in one sentence"}' | jq

Expected: JSON with a reply field containing the model response.

Troubleshooting

ErrorFix
401 from OpenAICheck OPENAI_API_KEY is set and valid
Bean ChatClient missingEnsure spring-ai-openai-spring-boot-starter is on classpath
CORS blocked from AngularAdd @CrossOrigin or configure WebMvcConfigurer

Next steps

Source code: munonye-ai-chat-spring-angular on GitHub

Related:
AI Developer Tutorials hub ·
Angular CRUD Part 1 ·
Spring AI overview

Kindson Munonye

Kindson Munonye is a software engineer and technical author specializing in Angular, Spring Boot, and microservices architecture. He publishes step-by-step tutorials with source code covering CRUD operations, reactive forms, CQRS, event sourcing, and REST API integration.GitHub · LinkedIn · About · YouTube

View all posts by Kindson Munonye →
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted