0
0
Spring Bootframework~5 mins

Handling path variables and query params together in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a path variable in Spring Boot?
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
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
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
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)
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
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.