A path variable is a part of the URL path that is treated as a variable. It is used to capture dynamic values from the URL, like an ID or name, and pass it to the controller method.
Click to reveal answer
beginner
How do you define a query parameter in a Spring Boot controller?
You define a query parameter by adding a method parameter annotated with @RequestParam. It captures values from the URL after the '?' symbol, like ?page=2.
Click to reveal answer
beginner
Show the annotation to capture a path variable named 'userId' in a Spring Boot controller method.
Use @PathVariable("userId") in the method parameter to capture the value from the URL path.
Click to reveal answer
intermediate
Can you use both @PathVariable and @RequestParam in the same Spring Boot controller method?
Yes, you can use both together to capture parts of the URL path and query parameters in the same method.
Click to reveal answer
intermediate
What happens if a required @RequestParam is missing in the request?
Spring Boot will return a 400 Bad Request error unless you set required=false or provide a defaultValue in the @RequestParam annotation.
Click to reveal answer
Which annotation is used to capture a path variable in Spring Boot?
A@PathVariable
B@RequestParam
C@RequestBody
D@RequestHeader
✗ Incorrect
The @PathVariable annotation captures dynamic parts of the URL path.
How do you capture a query parameter named 'sort' in a Spring Boot controller?
A@PathVariable("sort") String sort
B@RequestHeader("sort") String sort
C@RequestParam("sort") String sort
D@RequestBody String sort
✗ Incorrect
Use @RequestParam("sort") to capture query parameters from the URL.
What is the correct way to define a method that handles URL /users/{id} and query param ?active=true?
AUse @PathVariable for both id and active
BUse @PathVariable for id and @RequestParam for active
CUse @RequestParam for both id and active
DUse @RequestBody for both id and active
✗ Incorrect
Path variables come from the URL path, query params come after '?'. Use @PathVariable and @RequestParam respectively.
If a query parameter is optional, how do you declare it in Spring Boot?
A@RequestParam(required = false)
B@RequestParam(required = true)
C@PathVariable(required = false)
D@RequestBody(required = false)
✗ Incorrect
Setting required=false makes the query parameter optional.
What HTTP status code does Spring Boot return if a required query parameter is missing?
A500 Internal Server Error
B404 Not Found
C200 OK
D400 Bad Request
✗ Incorrect
Missing required query parameters cause a 400 Bad Request error.
Explain how to handle both path variables and query parameters in a single Spring Boot controller method.
Think about how URLs are structured with paths and queries.
You got /4 concepts.
Describe what happens if a required query parameter is missing in a Spring Boot request and how to avoid errors.
Consider user mistakes and how your app should respond.
You got /4 concepts.
Practice
(1/5)
1. In Spring Boot, which annotation is used to extract a value from the URL path segment in a controller method?
easy
A. @PathVariable
B. @RequestParam
C. @RequestBody
D. @RequestHeader
Solution
Step 1: Understand URL parts
The URL path segment is the part of the URL that defines the resource location, like /users/{id}.
Step 2: Identify the correct annotation
@PathVariable is used to bind a method parameter to a URI template variable in the path.
Final Answer:
@PathVariable -> Option A
Quick Check:
Path segment value = @PathVariable [OK]
Hint: Use @PathVariable for URL path parts, not query strings [OK]
Common Mistakes:
Confusing @RequestParam with @PathVariable
Using @RequestBody for path variables
Trying to get path variables from headers
2. Which of the following method signatures correctly handles a path variable id and a query parameter sort in Spring Boot?
easy
A. public String getUser(@PathVariable String id, @RequestParam String sort)
B. public String getUser(@RequestParam String id, @PathVariable String sort)
C. public String getUser(@RequestParam String id, @RequestParam String sort)
D. public String getUser(@PathVariable String id, @PathVariable String sort)
Solution
Step 1: Match annotations to parameter types
Path variables come from the URL path, so use @PathVariable for id. Query parameters come after ?, so use @RequestParam for sort.
Step 2: Check method signature correctness
public String getUser(@PathVariable String id, @RequestParam String sort) correctly uses @PathVariable for id and @RequestParam for sort.
Final Answer:
public String getUser(@PathVariable String id, @RequestParam String sort) -> Option A
What will be the output when a client requests /items/42?order=desc?
medium
A. "42-asc"
B. "42-desc"
C. "desc-42"
D. Runtime error due to missing parameter
Solution
Step 1: Identify values from URL
Path variable itemId is "42" from /items/42. Query parameter order is "desc" from ?order=desc.
Step 2: Understand method return
The method returns itemId + "-" + order, so "42-desc".
Final Answer:
"42-desc" -> Option B
Quick Check:
Path variable + query param = "42-desc" [OK]
Hint: Path variable fills {itemId}, query param fills order value [OK]
Common Mistakes:
Ignoring query parameter and using default
Mixing order of concatenation
Expecting error when query param is present
4. Consider this controller method:
@GetMapping("/books/{bookId}")
public String getBook(@PathVariable int bookId, @RequestParam int page) {
return "Book: " + bookId + ", Page: " + page;
}
What error will occur if a client calls /books/abc?page=2?
medium
A. 404 Not Found because path variable missing
B. 500 Internal Server Error due to null page
C. 400 Bad Request due to type mismatch
D. No error, returns "Book: abc, Page: 2"
Solution
Step 1: Check path variable type
The method expects bookId as int, but URL provides "abc" which is not a number.
Step 2: Understand Spring Boot behavior on type mismatch
Spring Boot returns 400 Bad Request when it cannot convert path variable to required type.
Final Answer:
400 Bad Request due to type mismatch -> Option C
Quick Check:
Type mismatch in @PathVariable causes 400 error [OK]
Hint: Non-numeric path variable for int causes 400 error [OK]
Common Mistakes:
Expecting 404 Not Found instead of 400
Assuming null page causes error
Thinking method returns string with invalid input
5. You want to create a Spring Boot endpoint that handles a user ID as a path variable and optionally accepts a query parameter filter. If filter is missing, it should default to "all". Which method signature correctly implements this behavior?
hard
A. public String getUser(@PathVariable String userId, @RequestParam Optional<String> filter)
B. public String getUser(@RequestParam String userId, @RequestParam String filter)
C. public String getUser(@PathVariable String userId, @RequestParam(required = true) String filter)
D. public String getUser(@PathVariable String userId, @RequestParam(defaultValue = "all") String filter)
Solution
Step 1: Identify path variable usage
User ID is part of the URL path, so use @PathVariable for userId.
Step 2: Handle optional query parameter with default
Use @RequestParam with defaultValue = "all" to provide a default when filter is missing.
Final Answer:
public String getUser(@PathVariable String userId, @RequestParam(defaultValue = "all") String filter) -> Option D
Quick Check:
Default query param value = @RequestParam(defaultValue) [OK]
Hint: Use @RequestParam(defaultValue) for optional query params [OK]
Common Mistakes:
Using @RequestParam(required = true) for optional params
Using @RequestParam without defaultValue causing errors
Confusing @RequestParam and @PathVariable roles
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.