Challenge - 5 Problems
SpringBoot @RequestMapping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the base URL path for this controller?
Given the following Spring Boot controller code, what is the base URL path that all methods inside this controller will respond to?
Spring Boot
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api/v1/users") public class UserController { @RequestMapping("/list") public String listUsers() { return "User list"; } @RequestMapping("/detail") public String userDetails() { return "User details"; } }
Attempts:
2 left
💡 Hint
Look at the @RequestMapping annotation on the class level.
✗ Incorrect
The @RequestMapping annotation on the class sets the base path for all methods inside the controller. Here, it is /api/v1/users. The methods add to this base path.
❓ state_output
intermediate2:00remaining
What URL path does the listUsers() method respond to?
Using the same controller code, what is the full URL path that the listUsers() method will respond to?
Spring Boot
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api/v1/users") public class UserController { @RequestMapping("/list") public String listUsers() { return "User list"; } @RequestMapping("/detail") public String userDetails() { return "User details"; } }
Attempts:
2 left
💡 Hint
Combine the class-level and method-level @RequestMapping paths.
✗ Incorrect
The full URL path is the base path from the class /api/v1/users plus the method path /list, resulting in /api/v1/users/list.
📝 Syntax
advanced2:00remaining
Which option correctly sets a base path with multiple paths in @RequestMapping?
You want your controller to respond to two base paths: "/api/v1/users" and "/users". Which @RequestMapping annotation syntax correctly sets this?
Attempts:
2 left
💡 Hint
Check how to specify multiple paths in an array for @RequestMapping.
✗ Incorrect
To specify multiple paths, use an array of strings inside curly braces: {"/api/v1/users", "/users"}. Option A is correct.
🔧 Debug
advanced2:00remaining
What error occurs with this incorrect @RequestMapping usage?
Consider this controller code snippet:
@RestController
@RequestMapping("/api/v1/users")
public class UserController {
@RequestMapping
public String listUsers() {
return "User list";
}
@RequestMapping("/detail")
public String userDetails() {
return "User details";
}
}
What happens when you try to access "/api/v1/users" in the browser?
Attempts:
2 left
💡 Hint
Method-level @RequestMapping without a path matches the base path.
✗ Incorrect
If a method has @RequestMapping without a path, it matches the base path set by the class-level @RequestMapping. So accessing "/api/v1/users" returns "User list".
🧠 Conceptual
expert3:00remaining
How does @RequestMapping at class and method levels combine for HTTP methods?
Given this controller snippet:
@RestController
@RequestMapping(path = "/api/v1/items", method = RequestMethod.GET)
public class ItemController {
@RequestMapping(path = "/create", method = RequestMethod.POST)
public String createItem() {
return "Item created";
}
@RequestMapping(path = "/list")
public String listItems() {
return "Item list";
}
}
Which HTTP methods and paths are valid for these two methods?
Attempts:
2 left
💡 Hint
Method-level @RequestMapping overrides HTTP method from class-level if specified.
✗ Incorrect
The class-level @RequestMapping sets GET as default method for all paths. The createItem method overrides this with POST. The listItems method inherits GET from the class.