Challenge - 5 Problems
Path & Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when accessing a Spring Boot endpoint with both path variable and query parameter?
Consider the following Spring Boot controller method:
What will be the response body when a client sends a GET request to
@GetMapping("/users/{id}")
public String getUser(@PathVariable String id, @RequestParam(required = false) String filter) {
return "User: " + id + ", Filter: " + (filter == null ? "none" : filter);
}What will be the response body when a client sends a GET request to
/users/42?filter=active?Spring Boot
@GetMapping("/users/{id}") public String getUser(@PathVariable String id, @RequestParam(required = false) String filter) { return "User: " + id + ", Filter: " + (filter == null ? "none" : filter); }
Attempts:
2 left
💡 Hint
Remember that @PathVariable extracts the part of the URL path, and @RequestParam extracts query parameters.
✗ Incorrect
The path variable 'id' captures '42' from the URL path, and the query parameter 'filter' captures 'active'. The method returns both values combined in the string.
📝 Syntax
intermediate2:00remaining
Which method signature correctly handles a path variable and an optional query parameter in Spring Boot?
You want to create a GET endpoint that accepts a path variable 'category' and an optional query parameter 'sort'. Which method signature is correct?
Attempts:
2 left
💡 Hint
Path variables come from the URL path, query parameters come after '?' and can be optional.
✗ Incorrect
Option D correctly uses @PathVariable for 'category' and marks 'sort' as optional with @RequestParam(required = false). Option D requires 'sort' always, B swaps annotations incorrectly, and D uses @RequestParam for 'category' which should be a path variable.
🔧 Debug
advanced2:00remaining
Why does this Spring Boot method fail to bind the path variable correctly?
Given this controller method:
What error or behavior will occur when accessing
@GetMapping("/products/{id}")
public String getProduct(@RequestParam String id) {
return "Product " + id;
}What error or behavior will occur when accessing
/products/10?Spring Boot
@GetMapping("/products/{id}") public String getProduct(@RequestParam String id) { return "Product " + id; }
Attempts:
2 left
💡 Hint
Check if the method parameter matches the URL path variable or query parameter.
✗ Incorrect
The method expects 'id' as a query parameter (@RequestParam), but the URL provides it as a path variable. Since no query parameter 'id' is sent, Spring returns 400 Bad Request.
❓ state_output
advanced2:00remaining
What is the output of this Spring Boot controller method when called with /orders/5?status=shipped?
Analyze this method:
What will be the response body for the request
@GetMapping("/orders/{orderId}")
public String getOrder(@PathVariable int orderId, @RequestParam(defaultValue = "pending") String status) {
return "Order " + orderId + " is " + status;
}What will be the response body for the request
/orders/5?status=shipped?Spring Boot
@GetMapping("/orders/{orderId}") public String getOrder(@PathVariable int orderId, @RequestParam(defaultValue = "pending") String status) { return "Order " + orderId + " is " + status; }
Attempts:
2 left
💡 Hint
The query parameter 'status' overrides the default value.
✗ Incorrect
The path variable 'orderId' is 5, and the query parameter 'status' is 'shipped', so the method returns 'Order 5 is shipped'.
🧠 Conceptual
expert2:00remaining
Which statement best describes handling path variables and query parameters together in Spring Boot?
Select the most accurate statement about using @PathVariable and @RequestParam together in a Spring Boot controller method.
Attempts:
2 left
💡 Hint
Think about where each annotation gets its data from in the URL.
✗ Incorrect
Option A correctly explains that @PathVariable gets data from the URL path segments, and @RequestParam gets data from the query string. They can be combined in one method.