Problem statement
Build a REST API that dispatches a notification to the sender bean matching the requested channel.
Requirements
NotificationConfig
There are three channels, email, sms, and push, each served by the matching provided sender.
- Turn this class into a configuration class
- Declare each sender as its own bean. The sender classes are not components, so Spring will not create these beans by itself
- Name each bean after its channel: the EmailSender bean is named email, the SmsSender bean is named sms, and the PushSender bean is named push
NotifyController
- Inject the sender beans through the constructor as a map keyed by bean name, which is the channel name
- Inject the default channel through the constructor, read from the notifications.default-channel property
POST /notify
- Accept a JSON body carrying a channel and a message
- Route the request to the sender whose channel name exactly matches the request's channel, and use that sender to send the message
- If the channel matches no sender's channel name, whether unknown, blank, or differing in case, route to the sender for the configured default channel instead
- Return 200 OK with a JSON object holding channel, the channel actually used after any fallback, and result, the string returned by the chosen sender
Sample Requests & Responses
POST /notify
Request:
{
"channel": "sms",
"message": "Your code is 1234"
}
Response (200 OK):
{
"channel": "sms",
"result": "SMS: Your code is 1234"
}
POST /notify
Request:
{
"channel": "fax",
"message": "Your code is 1234"
}
Response (200 OK):
{
"channel": "email",
"result": "EMAIL: Your code is 1234"
}
