Boost Your Spring Boot Apps with Generative AI Using Spring AI

This tutorial walks you through adding OpenAI's generative capabilities to a Spring Boot application with Spring AI, covering project setup, API key configuration, controller creation, simple and advanced prompt usage, and how to test the resulting AI-powered endpoints.

Programmer DD
Programmer DD
Programmer DD
Boost Your Spring Boot Apps with Generative AI Using Spring AI

Previously a video introduced Spring AI; this article provides a step‑by‑step guide for integrating OpenAI's generative AI into a Spring Boot application.

Hands‑on Demo

Step 1: Create a basic Spring Boot project with your preferred IDE. If you are new to Spring Boot, start with a quick‑start tutorial.

Step 2: Open application.properties and add your OpenAI API key:

spring.ai.openai.api-key=<YOUR_OPENAI_API_KEY>

Step 3: Create OpenAIController.java:

@RestController
@RequestMapping("/api/v1")
public class OpenAIController {
    private final AiClient aiClient;
    public OpenAIController(AiClient aiClient) {
        this.aiClient = aiClient;
    }
}

Step 4: Use the AiClient to return a simple completion:

@GetMapping("/completion")
public String completion(@RequestParam(value = "message") String message) {
    return this.aiClient.generate(message);
}

This is the most basic example. For more accurate results you should use a Prompt:

@GetMapping("/completion")
public AiResponse completion(@RequestParam(value = "message") String message) {
    PromptTemplate promptTemplate = new PromptTemplate("translate the given english sentence sentence into french {query}");
    Prompt prompt = promptTemplate.create(Map.of("query", message));
    return this.aiClient.generate(prompt);
}

The PromptTemplate creates a template, and the resulting Prompt is sent to the AI model.

Behind the scenes, a Prompt is a collection of Message objects, each representing a part of the conversation. This structure lets you compose complex interactions with the model.

More complex usage with a system prompt and multiple messages:

@GetMapping("/completion")
public List<Generation> completion(@RequestParam(value = "message") String message) {
    String systemPrompt = ""
        "You are a helpful AI assistant that helps people translate given text from english to french.
"
        "Your name is TranslatePro.
"
        "You should reply to the user's request with your name and also in the style of a professional.
"
        "";
    SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemPrompt);
    Message systemMessage = systemPromptTemplate.createMessage();

    PromptTemplate promptTemplate = new PromptTemplate("translate the given english sentence sentence into french {query}");
    Message userMessage = promptTemplate.createMessage(Map.of("query", message));

    Prompt prompt = new Prompt(List.of(systemMessage, userMessage));
    return this.aiClient.generate(prompt).getGenerations();
}

Here the Prompt combines a system message and a user message to guide the model toward better results.

After implementing these APIs, start the application and use an API testing tool (e.g., Postman or curl) to call the endpoints and experience the power of generative AI in your Spring app.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentSpring BootOpenAISpring AIGenerative AI
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.