Complete the code to bind the URL parameter to the method argument.
@GetMapping("/users/{id}") public String getUser(@[1] String id) { return "User ID: " + id; }
The @PathVariable annotation binds the URL path parameter {id} to the method argument.
Complete the code to specify the path variable name explicitly.
@GetMapping("/products/{productId}") public String getProduct(@PathVariable("[1]") String id) { return "Product ID: " + id; }
The string inside @PathVariable must match the name in the URL path {productId}.
Fix the error in the method parameter to correctly bind the path variable.
@GetMapping("/orders/{orderId}") public String getOrder(@PathVariable("[1]") String id) { return "Order ID: " + id; }
The @PathVariable annotation requires the path variable name as a string in parentheses.
Fill both blanks to create a method that binds two path variables.
@GetMapping("/users/{userId}/posts/{postId}") public String getPost(@PathVariable("[1]") String user, @PathVariable("[2]") String post) { return "User: " + user + ", Post: " + post; }
Each @PathVariable must match the corresponding name in the URL path.
Fill all three blanks to create a method that binds three path variables with explicit names.
@GetMapping("/shops/{shopId}/categories/{categoryId}/items/{itemId}") public String getItem(@PathVariable("[1]") String shop, @PathVariable("[2]") String category, @PathVariable("[3]") String item) { return "Shop: " + shop + ", Category: " + category + ", Item: " + item; }
Each @PathVariable annotation must match the corresponding path variable name in the URL.