StaffCoder
Spring Boot
Spring BootSpring Boot 4.1Java 25
12 modules · 48 challengesSign up
Module 1 · Beans & Dependency Injection

Notification Dispatch Selector

easyCollection InjectionNamed Beans75%3 of 4 solved

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"
}

More in Beans & Dependency Injection

See the module
Coffee Shop Order PrinterThis problem covers connecting the parts of a coffee shop ordering app so each one is handed what it needs, instead of building it itself.44%36 of 81 solvedeasyTemperature Unit Provider SwitcherThis problem covers registering two temperature converters and telling Spring which one to use by default and which one to hand over when it is asked for by name.59%10 of 17 solvedeasyFeature Flag Bean RegistryThis problem covers creating a set of feature on/off switches as Spring beans, filling each one from the settings file, and passing them all to the class that reports them.80%4 of 5 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.