0
0
Spring Bootframework~10 mins

Handling path variables and query params together in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Handling path variables and query params together
HTTP Request Received
Extract Path Variable
Extract Query Parameters
Call Controller Method with Both
Process Data Using Both
Return Response
The server receives a request, extracts path variables and query parameters, then calls the controller method using both to process and respond.
Execution Sample
Spring Boot
@GetMapping("/users/{id}")
public String getUser(@PathVariable String id, @RequestParam(required = false) String filter) {
    return "User: " + id + ", Filter: " + filter;
}
This code handles a GET request with a path variable 'id' and an optional query parameter 'filter', returning a combined string.
Execution Table
StepActionPath Variable 'id'Query Param 'filter'Method CalledReturned Output
1Request received: GET /users/42?filter=active42activegetUser("42", "active")User: 42, Filter: active
2Request received: GET /users/7?filter=all7allgetUser("7", "all")User: 7, Filter: all
3Request received: GET /users/99?filter=99getUser("99", "")User: 99, Filter:
4Request received: GET /users/5 (no filter param)5null (missing)getUser("5", null)User: 5, Filter: null
💡 Execution stops after method returns response or error if required params missing.
Variable Tracker
VariableStartAfter 1After 2After 3After 4
idnull427995
filternullactiveallnull
Key Moments - 2 Insights
What happens if the query parameter 'filter' is missing in the request?
If 'filter' is missing, the method may receive null or throw an error depending on if @RequestParam is required. See execution_table row 4 where missing 'filter' leads to null value passed to the method.
How does Spring Boot know which part of the URL is the path variable and which is the query parameter?
Spring Boot uses @PathVariable to extract parts from the URL path (like /users/{id}) and @RequestParam to extract query parameters after '?'. This is shown in execution_table where 'id' comes from path and 'filter' from query.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'filter' at step 3?
A"active"
B"" (empty string)
C"all"
Dnull
💡 Hint
Check the 'Query Param filter' column at step 3 in the execution_table.
At which step does the method get called with a missing query parameter?
AStep 4
BStep 2
CStep 3
DStep 1
💡 Hint
Look for 'null (missing)' in the 'Query Param filter' column in execution_table.
If the path variable 'id' was changed to '100' in step 2, what would be the returned output?
A"User: 7, Filter: all"
B"User: 100, Filter: active"
C"User: 100, Filter: all"
D"User: 7, Filter: active"
💡 Hint
Refer to step 2 in execution_table and replace 'id' with 100 keeping 'filter' same.
Concept Snapshot
Use @PathVariable to get parts of the URL path.
Use @RequestParam to get query parameters after '?'.
Both can be used together in one controller method.
Missing query params may cause errors unless marked optional.
Example: /users/{id}?filter=active
Method signature: getUser(@PathVariable String id, @RequestParam(required = false) String filter)
Full Transcript
This visual execution shows how Spring Boot handles requests with both path variables and query parameters. The server receives a request like GET /users/42?filter=active. It extracts the path variable 'id' as 42 and the query parameter 'filter' as 'active'. Then it calls the controller method getUser with these values. The method returns a string combining both values. If the query parameter is missing, the method may throw an error or handle a default value. This is important to know when designing APIs. The execution table traces multiple requests showing how variables change and how the method output depends on inputs. The key moments clarify common confusions about missing parameters and how Spring Boot distinguishes path variables from query parameters. The quiz tests understanding by asking about variable values at specific steps and effects of changing inputs.