Request Mapping
Spring MVC selects a handler using path, HTTP method, media types, headers, and sometimes query parameters.
Introduction
Spring MVC selects a handler using path, HTTP method, media types, headers, and sometimes query parameters. Fine-grained mapping turns the framework into a contract enforcer for Orders.
Prefer composed annotations: @GetMapping, @PostMapping, @PutMapping, @PatchMapping, @DeleteMapping. A class-level @RequestMapping("/api/orders") shares the prefix.
consumes and produces declare JSON expectations. headers can require an idempotency key on create. params can specialize a method when a query flag is present — use sparingly.
Ambiguous mappings fail at startup. That failure is better than a silent wrong handler in production.
Purpose of this lesson
Map Orders endpoints by verb, path, and media type, and use header/param constraints only when they encode a real API rule.
Understanding the topic
Mapping attributes are part of the public contract.
- path / value: resource location under the class prefix.
- method: carried by composed annotations.
- consumes / produces:
application/jsonfor Orders JSON APIs. - headers: require clients to send agreed headers (for example idempotency).
- params: optional specialization; prefer separate paths when clarity suffers.
Step-by-step explanation
- Declare class-level
/api/orders. - Add
@GetMapping("/{id}")and@PostMappingwithconsumes/producesJSON. - Require
X-Idempotency-Keyon create viaheaders. - Start the app; fix any ambiguous mapping errors before writing business logic.
Informative example
Orders create with media type and idempotency header.
@RestController@RequestMapping("/api/orders")public class OrderController {private final OrderService orders;public OrderController(OrderService orders) { this.orders = orders; }@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE,headers = "X-Idempotency-Key")@ResponseStatus(HttpStatus.CREATED)OrderView create(@RequestHeader("X-Idempotency-Key") String idempotencyKey,@Valid @RequestBody CreateOrderRequest body) {return orders.place(idempotencyKey, body);}}
Real-world use
Checkout retries happen on flaky mobile networks. Orders create is safer when the mapping demands X-Idempotency-Key and the service deduplicates by that key — the mapping makes the requirement visible in OpenAPI and in 400s when the header is missing.
Security implications
Mapping constraints are not authorization.
- Requiring a header does not prove the caller is allowed to place an order.
- Do not put secrets in custom headers that intermediaries log by default.
- Reject unexpected content types so form posts cannot bypass JSON validators accidentally.
Best practices
- Pin JSON consumes/produces on write endpoints.
- Fail startup on ambiguous mappings; never ignore the error.
- Document required headers in the API description the same day you enforce them.
Common mistakes
- Two methods that match the same verb+path without distinguishing consumes/params.
- Using only
@RequestMappingwithout a method — accidental acceptance of DELETE. - Overloading one path with many
paramsvariants until nobody can read the API.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionWhy prefer @GetMapping over @RequestMapping for reads?+
Answer
2IntermediateQuestionWhat do consumes and produces control?+
Answer
Content-Type values a handler accepts and which Accept/produced media types it handles — typically application/json for Orders.3AdvancedQuestionWhen is a headers= mapping constraint useful?+
Answer
Hands-on exercise
Lab. Add the create mapping with X-Idempotency-Key. Curl once with the header and once without; confirm the second call does not hit the service happy path.
Summary
Request mapping is how Orders declares HTTP contracts in code. Next you bind path and query inputs into typed method parameters.