0
0
Spring Bootframework~10 mins

Handling path variables and query params together 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 extract a path variable named 'id' in a Spring Boot controller method.

Spring Boot
@GetMapping("/items/[1]")
public String getItem(@PathVariable String id) {
    return "Item ID: " + id;
}
Drag options to blanks, or click blank then click option'
A/{id}
B/id
C{id}
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the curly braces around the variable name.
Using the variable name without slashes or braces.
2fill in blank
medium

Complete the method parameter to extract a query parameter named 'filter' in a Spring Boot controller.

Spring Boot
public String getItems(@RequestParam(required = false) String [1]) {
    return "Filter: " + filter;
}
Drag options to blanks, or click blank then click option'
Avalue
Bid
Cfilter
Dparam
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different name than the query parameter.
Omitting the @RequestParam annotation.
3fill in blank
hard

Fix the error in the annotation to correctly bind both path variable 'userId' and query parameter 'sort'.

Spring Boot
@GetMapping("/users/[1]")
public String getUser(@PathVariable String userId, @RequestParam String sort) {
    return userId + ": " + sort;
}
Drag options to blanks, or click blank then click option'
A/:userId
B/{userId}
C/userId
D/<userId>
Attempts:
3 left
💡 Hint
Common Mistakes
Using colon or angle brackets instead of curly braces.
Missing braces around the path variable.
4fill in blank
hard

Fill both blanks to define a method that handles path variable 'category' and optional query parameter 'page'.

Spring Boot
@GetMapping("/products/[1]")
public String listProducts(@PathVariable String [2], @RequestParam(required = false) Integer page) {
    return "Category: " + category + ", Page: " + page;
}
Drag options to blanks, or click blank then click option'
A{category}
Bcategory
Cpage
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatch between path variable name in URL and method parameter.
Forgetting curly braces in the URL.
5fill in blank
hard

Fill all three blanks to create a method that handles path variables 'orderId' and 'itemId' and a query parameter 'status'.

Spring Boot
@GetMapping("/orders/[1]/items/[2]")
public String getOrderItem(@PathVariable String [3], @PathVariable String itemId, @RequestParam String status) {
    return "Order: " + orderId + ", Item: " + itemId + ", Status: " + status;
}
Drag options to blanks, or click blank then click option'
A{orderId}
BorderId
CitemId
D{itemId}
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names or missing curly braces in the URL.
Mismatch between method parameter names and path variable names.