0
0
Spring Bootframework~10 mins

@PathVariable for URL parameters in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to bind the URL parameter to the method argument.

Spring Boot
@GetMapping("/users/{id}")
public String getUser(@[1] String id) {
    return "User ID: " + id;
}
Drag options to blanks, or click blank then click option'
ARequestHeader
BRequestParam
CRequestBody
DPathVariable
Attempts:
3 left
💡 Hint
Common Mistakes
Using @RequestParam instead of @PathVariable
Forgetting to add the annotation
Using @RequestBody for URL parameters
2fill in blank
medium

Complete the code to specify the path variable name explicitly.

Spring Boot
@GetMapping("/products/{productId}")
public String getProduct(@PathVariable("[1]") String id) {
    return "Product ID: " + id;
}
Drag options to blanks, or click blank then click option'
Aid
Bpid
CproductId
Dproduct
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different name than the URL path variable
Omitting the annotation parameter when names differ
3fill in blank
hard

Fix the error in the method parameter to correctly bind the path variable.

Spring Boot
@GetMapping("/orders/{orderId}")
public String getOrder(@PathVariable("[1]") String id) {
    return "Order ID: " + id;
}
Drag options to blanks, or click blank then click option'
AorderId
B(orderId)
C"orderId"
D("orderId")
Attempts:
3 left
💡 Hint
Common Mistakes
Missing parentheses
Missing quotes around the variable name
Using variable name without quotes
4fill in blank
hard

Fill both blanks to create a method that binds two path variables.

Spring Boot
@GetMapping("/users/{userId}/posts/{postId}")
public String getPost(@PathVariable("[1]") String user, @PathVariable("[2]") String post) {
    return "User: " + user + ", Post: " + post;
}
Drag options to blanks, or click blank then click option'
AuserId
Bpost
CpostId
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the path variable names
Using variable names instead of path variable names
5fill in blank
hard

Fill all three blanks to create a method that binds three path variables with explicit names.

Spring Boot
@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;
}
Drag options to blanks, or click blank then click option'
AshopId
BcategoryId
CitemId
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names instead of path variable names
Mixing up the order of path variables