You're reading for free via Master Spring Ter's Friend Link. Become a member to access the best of Medium.

Member-only story

Creating an AI Agent with Spring Boot: A Real-World Use Case

Master Spring Ter
3 min readFeb 3, 2025

--

for free reading -> https://master-spring-ter.medium.com/creating-an-ai-agent-with-spring-boot-a-real-world-use-case-620d02b23656?sk=da2dfa7400c50f661a1534d5b7b322c4

Introduction

With the rise of AI-driven applications, integrating AI agents into backend services is essential for automating workflows, enhancing customer support, and powering intelligent decision-making. In this article, we’ll build an AI-powered customer support chatbot using Spring Boot. The chatbot leverages OpenAI’s GPT model (via a hypothetical Spring AI Starter dependency) to generate dynamic responses.

Why Use Spring Boot for AI Agents?

Spring Boot provides a robust framework for scalable web applications. When combined with AI, it simplifies integration with external APIs, databases, and services.

Key Benefits:

  • Scalability: Effortlessly handles concurrent requests.
  • Microservices Support: Integrates well into a microservices architecture.
  • Easy AI Integration: Spring AI Starter (or similar libraries) simplifies working with AI APIs like OpenAI.
  • Spring Security: Secures API interactions.

Project Setup

Step 1: Create a Spring Boot Application

Generate a Spring Boot project using Spring Initializr with these dependencies:

  • Spring Boot Actuator — for monitoring.
  • Spring Security — for securing API calls.
  • Lombok — to reduce boilerplate code.
  • Spring Boot WebSocket — for real-time communication.
  • Spring AI Starter — for AI integration (ensure you add the proper Maven/Gradle dependency).
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-openai</artifactId>
<version>1.0.0</version>
</dependency>

Step 2: Configure OpenAI API

Sign up for an OpenAI API key at openai.com and store it in your application.properties file:

spring.ai.openai.api-key=your_api_key_here

Step 3: Implement the AI Agent Service with Spring AI

Create a ChatService class to handle communication with OpenAI’s API. We’ve added error handling and logging (using Lombok’s @Slf4j) for a production-ready touch:

package com.example.chatbot.service;

import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class ChatService {
private final OpenAiChatClient openAiChatClient;

public ChatService(OpenAiChatClient openAiChatClient) {
this.openAiChatClient = openAiChatClient;
}

public String getResponse(String userMessage) {
try {
return openAiChatClient.call(userMessage);
} catch(Exception ex) {
log.error("Error calling OpenAI API", ex);
return "Sorry, I'm having trouble responding right now.";
}
}
}

Step 4: Create WebSocket Configuration

Set up WebSockets for real-time chatbot communication. Note: For production, restrict allowed origins instead of using "*".

package com.example.chatbot.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/chat").setAllowedOrigins("*").withSockJS();
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic");
registry.setApplicationDestinationPrefixes("/app");
}
}

Step 5: Create the WebSocket Controller for AI Chat

The controller listens for messages on /app/message and broadcasts responses to /topic/response.

package com.example.chatbot.controller;

import com.example.chatbot.service.ChatService;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
@Controller
public class ChatWebSocketController {
private final ChatService chatService;

public ChatWebSocketController(ChatService chatService) {
this.chatService = chatService;
}

@MessageMapping("/message")
@SendTo("/topic/response")
public String chat(String userMessage) {
return chatService.getResponse(userMessage);
}
}

Step 6: Integrating with a React Mobile App

Use SockJS and StompJS to connect your React app to the WebSocket server. Adjusted to send and receive plain text (since the server expects a simple string):

import SockJS from 'sockjs-client';
import Stomp from 'stompjs';

const socket = new SockJS('http://localhost:8080/chat');
const stompClient = Stomp.over(socket);
stompClient.connect({}, () => {
stompClient.subscribe('/topic/response', (response) => {
console.log("AI Response:", response.body);
});
});
const sendMessage = (message) => {
stompClient.send("/app/message", {}, message);
};

Conclusion

By combining Spring Boot, Spring AI Starter, and WebSockets, you can quickly build an interactive, real-time AI-powered chatbot. Integrating this backend with a React mobile app creates a seamless customer support experience. Remember, while this example is great for getting started, consider adding more robust error handling, security measures, and logging when moving to production.

Master Spring Ter
Master Spring Ter

Written by Master Spring Ter

https://chatgpt.com/g/g-dHq8Bxx92-master-spring-ter Specialized ChatGPT expert in Spring Boot, offering insights and guidance for developers.

No responses yet

Write a response