Spring Integration Routers Interview Questions and Answers
Master Spring Integration Routers with interview questions covering Message Router, PayloadTypeRouter, HeaderValueRouter, RecipientListRouter, MethodInvokingRouter, XPath Router, dynamic routing, and production best practices.
Introduction
In enterprise systems, not every message should be processed by the same service.
For example, a banking application receives different payment requests:
- Domestic Payment
- International Payment
- Credit Card Payment
- Loan Payment
- UPI Payment
Each payment must be routed to a different processing service.
Instead of writing large if-else or switch statements, Spring Integration provides Message Routers.
A Router examines a message and decides where it should go next.
Router Architecture
flowchart LR
Gateway --> InputChannel
InputChannel --> Router
Router --> PaymentService
Router --> LoanService
Router --> CardService
Q1. What is a Message Router?
Answer
A Message Router determines the destination channel for a message.
The routing decision can be based on
- Payload
- Header
- Message type
- Custom business logic
Benefits
- Loose coupling
- Flexible routing
- Easy extensibility
- Better maintainability
Q2. Why do we need Routers?
Without Router
if (paymentType == "DOMESTIC")
...
else if (...)
...
else if (...)
Business logic becomes tightly coupled with routing logic.
With Router
flowchart TD
IncomingMessage --> Router
Router --> DomesticChannel
Router --> InternationalChannel
Router --> LoanChannel
Routing becomes configurable and reusable.
Q3. What is PayloadTypeRouter?
PayloadTypeRouter routes messages based on the Java object type.
Example
@Bean
PayloadTypeRouter
router(){
return new
PayloadTypeRouter();
}
Example
Customer
↓
CustomerService
Order
↓
OrderService
Useful when multiple payload classes share the same channel.
Q4. What is HeaderValueRouter?
Routes messages using header values.
Example
region=US
↓
US Service
region=EU
↓
EU Service
Header Router
flowchart TD
Message --> HeaderRouter
HeaderRouter --> USChannel
HeaderRouter --> EuropeChannel
HeaderRouter --> AsiaChannel
Headers allow routing without changing payloads.
Q5. What is RecipientListRouter?
Routes one message to multiple recipients.
Example
Payment
↓
Audit
Notification
Analytics
Recipient List
flowchart TD
Payment --> RecipientListRouter
RecipientListRouter --> AuditService
RecipientListRouter --> EmailService
RecipientListRouter --> AnalyticsService
Ideal for event broadcasting.
Q6. What is MethodInvokingRouter?
Uses a Java method to determine the destination.
Example
public String
route(
Payment payment){
}
Spring calls the method and routes the message based on its return value.
Suitable for complex business rules.
Q7. What is XPath Router?
Used for XML payloads.
Example
<payment>
<country>
US
</country>
</payment>
XPath
/payment/country
Routes based on XML content.
Used in legacy enterprise systems.
Q8. Can Routers perform Dynamic Routing?
Yes.
Destination channels can be determined at runtime.
Example
VIP Customer
↓
Priority Service
Regular Customer
↓
Standard Service
Dynamic routing supports evolving business rules without changing application flow.
Q9. Router vs Filter
| Router | Filter |
|---|---|
| Chooses destination | Accepts or rejects message |
| Multiple outputs | Single output |
| Always routes | May discard |
| Branching logic | Validation logic |
Use
- Router → Select destination
- Filter → Validate messages
Q10. Router Best Practices
Keep Routing Logic Separate
Avoid embedding routing decisions in services.
Route Using Headers
When possible.
Use Method Routers
For complex business decisions.
Avoid Large if-else Blocks
Prefer configurable routers.
Monitor Routing
Log routing decisions for troubleshooting.
Banking Example
flowchart TD
PaymentGateway --> Router
Router --> DomesticPayments
Router --> InternationalPayments
Router --> CreditCardPayments
Router --> LoanPayments
DomesticPayments --> CoreBanking
InternationalPayments --> SWIFTGateway
Each payment type is processed independently.
Common Interview Questions
- What is a Router?
- Why use Routers?
- What is
PayloadTypeRouter? - What is
HeaderValueRouter? - What is
RecipientListRouter? - What is
MethodInvokingRouter? - What is an XPath Router?
- What is Dynamic Routing?
- Router vs Filter?
- Router best practices?
Quick Revision
| Router | Purpose |
|---|---|
| PayloadTypeRouter | Route by payload class |
| HeaderValueRouter | Route by header |
| RecipientListRouter | Multiple destinations |
| MethodInvokingRouter | Java method routing |
| XPath Router | XML routing |
| Dynamic Router | Runtime routing |
| Filter | Validate messages |
| Channel | Message destination |
| Header | Metadata for routing |
| Payload | Business data |
Router Lifecycle
sequenceDiagram
Gateway->>InputChannel: Send Message
InputChannel->>Router: Evaluate Message
Router->>BusinessRule: Determine Destination
BusinessRule-->>Router: Channel Name
Router->>SelectedChannel: Route Message
SelectedChannel->>ServiceActivator: Process
Production Example – Banking Payment Routing
A banking platform processes multiple payment types.
Requirements
- Domestic payments go to the Core Banking System.
- International payments go to the SWIFT gateway.
- Credit card payments go to the Card Processing System.
- Loan EMI payments go to the Loan Management System.
- Every successful payment should also notify the Audit and Notification services.
Implementation
- HeaderValueRouter routes based on the
paymentTypeheader. - MethodInvokingRouter handles VIP customer routing based on business rules.
- RecipientListRouter sends payment events to multiple downstream services.
public String determineChannel(
Payment payment
) {
return payment.isVip()
? "priorityChannel"
: "standardChannel";
}
flowchart LR
PaymentGateway --> HeaderValueRouter
HeaderValueRouter --> DomesticChannel
HeaderValueRouter --> InternationalChannel
HeaderValueRouter --> CardChannel
HeaderValueRouter --> LoanChannel
DomesticChannel --> CoreBanking
InternationalChannel --> SWIFT
CardChannel --> CardProcessor
LoanChannel --> LoanSystem
CoreBanking --> RecipientListRouter
RecipientListRouter --> AuditService
RecipientListRouter --> NotificationService
This architecture keeps routing logic centralized, improves maintainability, and enables new payment types to be added with minimal code changes.
Key Takeaways
- Message Routers determine the destination channel for messages based on payload, headers, or business rules.
- PayloadTypeRouter routes messages according to the Java object type.
- HeaderValueRouter makes routing decisions using message headers without modifying the payload.
- RecipientListRouter delivers a single message to multiple recipients for event broadcasting.
- MethodInvokingRouter supports custom Java logic for advanced routing scenarios.
- XPath Router enables routing based on XML document content.
- Routers should be responsible only for selecting destinations, while validation should be handled by Filters.
- Centralizing routing logic improves scalability, maintainability, and flexibility in enterprise integration solutions.