π Request Binding in Spring MVC¶
When an HTTP request hits your controller, Spring must bind the incoming data β path, query, form, or JSON body β to your method parameters.
There are three main βdoorsβ for that data:
| Layer | Annotation | Source | Typical Example |
|---|---|---|---|
| Identity | @PathVariable |
Path segment | /users/{id} |
| Filters / form inputs | @RequestParam |
Query string or form field | /users?active=true&page=2 |
| Payload | @RequestBody |
Request body (JSON/XML) | { "name": "Ana" } |
π§ Mental Model β βWhere does the data live?β¶
ββββββββββββββββββββββββββββββββ
β HTTP REQUEST β
β β
β GET /users/42?active=true β
β Body: { "name": "Ana" } β
βββββββββββββββ¬βββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββ
β Controller Method β
β β
β @PathVariable Long id β β /users/42
β @RequestParam Boolean active β β ?active=true
β @RequestBody UserDto body β β JSON body
ββββββββββββββββββββββββββββββββ
βοΈ Core Purposes¶
| Intent | Use | Example |
|---|---|---|
| Identify a resource | @PathVariable |
/users/{id} |
| Filter, sort, paginate | @RequestParam |
/users?active=true&page=2 |
| Create or update data | @RequestBody |
POST /users with {...} |
π§© Engine Differences¶
| Annotation | Bound from | Conversion Engine | Typical Content-Type |
|---|---|---|---|
@PathVariable |
URL template | ConversionService |
text/plain (implicit) |
@RequestParam |
Query/form field | ConversionService |
application/x-www-form-urlencoded, multipart/form-data |
@RequestBody |
Request body | HttpMessageConverter (e.g., Jackson JSON) |
application/json, application/xml |
βοΈ Comparison Summary¶
| Dimension | @PathVariable |
@RequestParam |
@RequestBody |
|---|---|---|---|
| Source | URL segment | Query string / form | HTTP body |
| When used | Identify which resource | Filter or adjust request | Send or receive structured data |
| Default scope | Required | Required (can default/optional) | Required |
| Type conversion | String β simple type | String β simple type | JSON/XML β object |
| Cache key relevance | Yes (different URL = different resource) | Yes (different query = different resource) | No (body not used in cache key) |
| Validation level | Simple | Simple | Deep (object graph) |
| Good for | REST paths | Filters, toggles, pagination | DTOs for create/update |
| Donβt use for | JSON bodies | Complex objects | Filters or identifiers |
π Real-World Mapping Example¶
@PostMapping("/shops/{shopId}/orders")
public OrderDto createOrder(
@PathVariable long shopId, // /shops/7
@RequestParam(defaultValue="false") boolean express, // ?express=true
@RequestBody OrderCreateRequest body // { "item": "Book", "qty": 3 }
) { ... }
π§ Folder Placement¶
cheatsheets/frameworks/spring/web/
ββ binding/
ββ overview.md # β this file
ββ path-variable.md
ββ request-param.md
ββ request-body.md
In MkDocs navigation:
- Spring Web:
- Binding (overview): cheatsheets/frameworks/spring/web/binding/overview.md
- PathVariable: cheatsheets/frameworks/spring/web/binding/path-variable.md
- RequestParam: cheatsheets/frameworks/spring/web/binding/request-param.md
- RequestBody: cheatsheets/frameworks/spring/web/binding/request-body.md
πͺ Next Layers¶
Once you master these three, your next frontier:
@ModelAttributeβ group multiple@RequestParams into a validated object.@ResponseBodyβ the reverse: converting your method return value to JSON/XML.@MatrixVariableβ niche but powerful for REST-like filtering in path segments.
Bottom line:
@PathVariablelocates the thing,@RequestParamdescribes how to get it,@RequestBodytells what it is.
Use this trinity as your mental compass for every REST endpoint you design.