SpringBoot Rest API + Gen AI Use cases
- Anand Nerurkar
- Apr 13
- 4 min read
Banking use case-
1.Customer Support Assistance
Customers often have queries about their accounts, transactions, or products. Traditional chatbots using rule-based systems offer limited help. The goal is to provide a real-time, intelligent support assistant that integrates with banking systems and understands natural language.

Solution: GenAI-powered Chat Assistant with Spring Boot Backend
1. Architecture Overview
Frontend (Web / Mobile App): Chat interface
Spring Boot Microservice:
Exposes REST APIs
Connects to core banking APIs (for account info, transactions, etc.)
Manages session, auth, role-based access
Integrates with a GenAI service (e.g., OpenAI, Azure OpenAI, or a fine-tuned LLM on-premise)
GenAI Layer:
Handles intent detection, entity extraction, and answer generation
Can be prompted with contextual data (via RAG or pre-processing)
RAG Layer (Retrieval-Augmented Generation):
Uses vector DB (like FAISS or Pinecone) + embeddings (OpenAI, HuggingFace)
Injects personalized or contextual banking info into the prompt
Observability & Logging:
ELK / Prometheus / Grafana
Logs GenAI prompts and responses for auditing
2. Spring Boot Responsibilities
Component | Role |
GenAiService | Sends user input to LLM API, handles retries, errors |
ChatController | REST API for frontend, manages request context |
BankingDataClient | Fetches real-time account/transaction data |
PromptBuilder | Constructs the GenAI prompt (with user data if RAG is enabled) |
ConversationStore | Maintains chat history (for multi-turn support) |
SecurityConfig | OAuth2/JWT auth with banking identity system |
3. Example Flow
User input: "What was my last credit card payment?"
Spring Boot:
Authenticates user
Retrieves credit card transaction data
Sends query + data to GenAI in the prompt:
User is asking: "What was my last credit card payment?" Their last payment was ₹15,000 on 2nd March at ICICI Bank.
GenAI:
Responds with a human-like answer:"Your last credit card payment of ₹15,000 was made on March 2nd to ICICI Bank. Anything else you'd like to check?"
Spring Boot: Sends response back to frontend.
4. Tech Stack
Spring Boot 3 (with WebFlux for async APIs)
OpenAI API (or Azure OpenAI / HuggingFace LLM)
PostgreSQL / Redis for context storage
ElasticSearch for logs, queries
Vector DB: FAISS / Pinecone (for RAG)
Security: Spring Security + OAuth2 + JWT
🚀 Benefits
Real-time, accurate, context-aware responses
Scalable microservice design
Can be expanded to wealth management, loan advisory, etc.
Traceable GenAI interactions via logging
Sample Code
==
✅ 1. Code Snippet: Spring Boot Integration with GenAI API
java
@Service
public class GenAiService {
@Value("${genai.api.url}")
private String apiUrl;
@Value("${genai.api.key}")
private String apiKey;
private final RestTemplate restTemplate = new RestTemplate();
public String getResponseFromGenAi(String prompt) {
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + apiKey);
headers.setContentType(MediaType.APPLICATION_JSON);
Map<String, Object> request = Map.of(
"model", "gpt-4",
"messages", List.of(Map.of("role", "user", "content", prompt))
);
HttpEntity<Map<String, Object>> entity = new HttpEntity<>(request, headers);
ResponseEntity<Map> response = restTemplate.postForEntity(apiUrl, entity, Map.class);
return ((Map)((List)response.getBody().get("choices")).get(0)).get("message").toString();
}
}
@RestController
@RequestMapping("/api/chat")
public class ChatController {
@Autowired
private GenAiService genAiService;
@Autowired
private BankingDataClient bankingDataClient;
@PostMapping
public ResponseEntity<String> chat(@RequestBody Map<String, String> request) {
String query = request.get("message");
String userId = request.get("userId");
String bankingContext = bankingDataClient.fetchLatestTransaction(userId);
String fullPrompt = "User asked: " + query + "\nRelevant data: " + bankingContext;
String response = genAiService.getResponseFromGenAi(fullPrompt);
return ResponseEntity.ok(response);
}
}
@Service
public class BankingDataClient {
public String fetchLatestTransaction(String userId) {
// Simulate fetching data from core banking APIs
return "Last payment of ₹15,000 to ICICI Bank on March 2nd.";
}
}
📊 2. Diagram (explained visually)
Here’s the flow already shown in the image:
Frontend ⟷ Spring Boot Microservice ⟷• Core Banking APIs• GenAI Service ⟷ RAG Layer• Observability & Logging
🚀 Optional Enhancements:
Add OpenTelemetry for observability
Use Spring WebClient for non-blocking calls (WebFlux)
Secure APIs using Spring Security + OAuth2
Enable RAG by integrating with a vector DB + embedding service
springboot-genai-chat/
├── src/
│ ├── main/
│ │ ├── java/com/anerurkar/genaibot/
│ │ │ ├── controller/ChatController.java
│ │ │ ├── service/GenAiService.java
│ │ │ ├── service/BankingDataClient.java
│ │ │ └── SpringbootGenaiBotApplication.java
│ │ └── resources/
│ │ ├── application.properties
│ └── test/
├── pom.xml
└── README.md
pom.xml
====
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.anerurkar</groupId>
<artifactId>springboot-genai-chat</artifactId>
<version>1.0.0</version>
<name>Spring Boot GenAI Chat Assistant</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
server.port=8080
genai.api.url=https://api.openai.com/v1/chat/completions
genai.api.key=your_api_key_here
readme
==
# Spring Boot + GenAI Chat Assistant
This is a sample banking use case that integrates Spring Boot with a GenAI API (like OpenAI) for real-time customer support.
## How it works
- Accepts chat input via REST
- Fetches contextual banking data
- Sends it to GenAI with prompt engineering
- Returns the response to the frontend
## How to Run
1. Clone the repo:
```bash
2.Use Case: AI-powered Loan Eligibility Advisor
🎯 Objective
Help customers understand their loan eligibility (home, car, personal) by analyzing their profile, transactions, and history, using GenAI for explanations.
🧠 GenAI Prompt Use
GenAI interprets financial behavior (salary, expenses, EMI patterns).
Explains eligibility criteria in conversational form.
Example Prompt:
vbnet
CopyEdit
Based on the user's salary of ₹1.2L/month, EMI of ₹25K, and a credit score of 780, assess eligibility for a personal loan of ₹5L and explain the decision.
🔧 Architecture Components
Spring Boot REST API
GenAI Service (OpenAI / Azure OpenAI)
Banking Profile Service (mock or real)
RAG Layer (ChromaDB or Redis) for adding financial regulations/FAQs
Swagger/OpenAPI
Docker + Docker Compose
React Frontend Chat UI
🔄 API Flow
User Input: "Can I get a personal loan for ₹5L?"
Spring Boot API collects:
Profile (income, age, occupation)
Transaction summary (salary credit, EMIs)
RAG context (RBI/NBFC norms)
GenAI builds a reasoned response.
✅ Sample Output
"You are likely eligible for a ₹5L personal loan based on your ₹1.2L salary and credit history. Your EMI burden is currently at 21%, which is within the safe range (< 40%)."
Comentarios