How to Add Generative AI to Your Spring Boot App with Spring AI
This tutorial walks you through creating a Spring Boot project, configuring OpenAI credentials, building a controller, and using Spring AI's Prompt and PromptTemplate classes to integrate generative AI capabilities into your backend application.
Hands‑On Demo
Step 1: Generate a basic Spring Boot project with your favorite IDE. If you are unfamiliar, follow a Spring Boot quick‑start guide.
Step 2: Open application.properties and configure 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 content based on the request:
@GetMapping("/completion")
public String completion(@RequestParam(value = "message") String message) {
return this.aiClient.generate(message);
}The simple example works, but real applications usually need a Prompt to obtain more accurate results. For example:
@GetMapping("/completion")
public AiResponse completion(@RequestParam(value = "message") String message) {
PromptTemplate promptTemplate = new PromptTemplate("translate the given english sentence into french {query}");
Prompt prompt = promptTemplate.create(Map.of("query", message));
return this.aiClient.generate(prompt);
}Here a PromptTemplate creates a template, and a Prompt is built from it using the user input.
The Prompt class holds a list of Message objects; each message represents a part of the prompt. Combining different messages, such as a system prompt and a user prompt, enables more sophisticated interactions with the AI model.
More complex usage can combine a system prompt and a user prompt:
@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 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();
}After implementing these APIs, start the application and invoke the endpoints with an API testing tool to experience the power of generative AI.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
