Challenge - 5 Problems
PathVariable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Spring Boot controller method?
Consider this Spring Boot controller method:
What will be the response body when a client requests
@GetMapping("/users/{id}")
public String getUser(@PathVariable String id) {
return "User ID: " + id;
}What will be the response body when a client requests
/users/42?Spring Boot
@GetMapping("/users/{id}") public String getUser(@PathVariable String id) { return "User ID: " + id; }
Attempts:
2 left
💡 Hint
The @PathVariable annotation binds the URL part to the method parameter.
✗ Incorrect
The {id} in the URL path is captured and passed as the method parameter 'id'. So when the URL is /users/42, the method receives '42' as the id and returns 'User ID: 42'.
📝 Syntax
intermediate2:00remaining
Which option correctly uses @PathVariable with a different parameter name?
Given the URL pattern
/items/{itemId}, which method signature correctly binds the path variable to a method parameter named id?Attempts:
2 left
💡 Hint
Use @PathVariable with the exact name of the path segment if the parameter name differs.
✗ Incorrect
When the method parameter name differs from the path variable name, you must specify the path variable name inside @PathVariable("itemId"). Option D does this correctly.
🔧 Debug
advanced2:00remaining
What error occurs with this controller method?
Examine this Spring Boot controller method:
What happens when a client requests
@GetMapping("/orders/{orderId}")
public String getOrder(@PathVariable int id) {
return "Order: " + id;
}What happens when a client requests
/orders/123?Spring Boot
@GetMapping("/orders/{orderId}") public String getOrder(@PathVariable int id) { return "Order: " + id; }
Attempts:
2 left
💡 Hint
Check if the method parameter name matches the path variable name or if @PathVariable specifies the name.
✗ Incorrect
The path variable is named 'orderId' but the method parameter is 'id' without specifying @PathVariable("orderId"). Spring cannot bind the path variable to the parameter, causing a 400 Bad Request error.
❓ state_output
advanced2:00remaining
What is the output of this controller with multiple @PathVariable parameters?
Given this controller method:
What is the response when the URL
@GetMapping("/books/{genre}/{id}")
public String getBook(@PathVariable String genre, @PathVariable int id) {
return genre + "-" + id;
}What is the response when the URL
/books/scifi/7 is requested?Spring Boot
@GetMapping("/books/{genre}/{id}") public String getBook(@PathVariable String genre, @PathVariable int id) { return genre + "-" + id; }
Attempts:
2 left
💡 Hint
Each @PathVariable binds to the matching segment in the URL path.
✗ Incorrect
The first path variable 'genre' gets 'scifi', and the second 'id' gets 7. The method returns 'scifi-7'.
🧠 Conceptual
expert2:00remaining
Which statement about @PathVariable is true?
Choose the correct statement about the use of @PathVariable in Spring Boot controllers.
Attempts:
2 left
💡 Hint
Think about advanced usage of @PathVariable with patterns.
✗ Incorrect
You can use regex in the path variable to capture multiple segments, e.g., @GetMapping("/files/{path:.+}") binds multiple path segments to 'path'.