Path Variables & Request Params
Orders identifiers belong in the path (/api/orders/42).
Introduction
Orders identifiers belong in the path (/api/orders/42). Filters, sort, and pagination belong in the query string (?status=PENDING&page=0&size=20).
@PathVariable binds URI template variables. @RequestParam binds query parameters (and form fields). Use required, defaultValue, and Jakarta constraints such as @Max on page size.
Type conversion is built in for primitives, enums, and dates with standard formats. Custom converters belong in configuration — not ad-hoc parsing in every controller.
Unbounded size parameters are a denial-of-service footgun against the database.
Purpose of this lesson
Bind Orders path ids and list filters correctly, with safe defaults and caps on page size.
Understanding the topic
Path means identity; query means options.
- @PathVariable: required pieces of the resource URI.
- @RequestParam: optional filters and paging.
- defaults:
page=0,size=20so/api/ordersworks bare. - validation:
@Max(100)on size; validate enums via type conversion failures or explicit checks. - names: keep query names stable; they become client contracts.
Step-by-step explanation
- Implement
GET /{id}with@PathVariable long id. - Implement
GET /api/orderswith optionalstatusand paging params. - Cap
sizewith@Max(100)and enable method validation if needed. - Curl with and without query params; confirm defaults.
Informative example
Get one + search.
@GetMapping("/{id}")OrderView getOne(@PathVariable long id) {return orders.get(id);}@GetMappingList<OrderView> search(@RequestParam(required = false) OrderStatus status,@RequestParam(defaultValue = "0") int page,@RequestParam(defaultValue = "20") @Max(100) int size) {return orders.search(status, page, size);}
Real-world use
Instructor ops lists PENDING Orders while mobile opens a single id. Mixing filters into path segments (/orders/PENDING) collides with numeric ids and breaks caching. Query params keep identity URLs clean.
Security implications
IDs in paths are still authorization problems.
- Check the caller may access order
id— do not trust obscurity. - Limit page size to protect DB CPU and memory.
- Do not reflect raw query strings into logs without redaction.
Best practices
- Document default page size in the API guide.
- Prefer enums for status query params.
- Keep path variable names aligned with URI templates for readability.
Common mistakes
- Using query params for the primary resource id.
- Allowing
size=100000without a max. - Silent stringly-typed status filters that ignore typos instead of 400ing.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionPath variable vs request param — when each?+
Answer
2IntermediateQuestionHow do you prevent huge page sizes?+
Answer
size, annotate with @Max, and enforce the same cap in the service even if a caller bypasses the controller.3IntermediateQuestionWhat happens when a query enum value is invalid?+
Answer
Hands-on exercise
Lab. Add search with default paging and @Max(100). Call with size=1000 and confirm validation fails once method validation is enabled.
Summary
Orders binds identity and filters at the controller edge with safe defaults. Next you align those handlers with REST resource semantics.