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

Temperature Unit Provider Switcher

easyBean QualifiersPrimary Bean59%10 of 17 solved

Problem statement

Connect the temperature converters to a REST API using bean selection and constructor injection.

Requirements

The GET /convert endpoint and both convert methods are already provided. Your task is to register the converters as beans and inject them into the controller.

CelsiusConverter

  • Register this class as the default converter, so a TemperatureConverter injected with no qualifier selects it
  • Its provided convert method returns the input unchanged, since the input is already Celsius

FahrenheitConverter

  • Register this class as a converter selectable by the qualifier fahrenheit
  • Its provided convert method returns celsius * 9 / 5 + 32

TemperatureController

  • Inject the default converter and the fahrenheit-qualified converter through the constructor
  • Once both are injected, the provided GET /convert endpoint uses the default converter for celsius (or no unit) and the fahrenheit converter for fahrenheit
Sample Requests & Responses

GET /convert?value=100&unit=fahrenheit

Response (200 OK):

{
  "unit": "fahrenheit",
  "value": 212.0
}

GET /convert?value=37&unit=fahrenheit

Response (200 OK):

{
  "unit": "fahrenheit",
  "value": 98.6
}

GET /convert?value=100

Response (200 OK):

{
  "unit": "celsius",
  "value": 100.0
}

GET /convert?value=25&unit=celsius

Response (200 OK):

{
  "unit": "celsius",
  "value": 25.0
}

GET /convert?value=100&unit=kelvin

Response (400 Bad Request):

{
  "error": "Unsupported unit: kelvin"
}

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 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 solvedeasyNotification Dispatch SelectorThis problem covers sending a message through the channel the user picked, and what to fall back to when the channel isn't recognized.75%3 of 4 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.