Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 14

    Request Mapping

    Spring MVC selects a handler using path, HTTP method, media types, headers, and sometimes query parameters.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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/json for 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

    1. Declare class-level /api/orders.
    2. Add @GetMapping("/{id}") and @PostMapping with consumes/produces JSON.
    3. Require X-Idempotency-Key on create via headers.
    4. Start the app; fix any ambiguous mapping errors before writing business logic.

    Informative example

    Orders create with media type and idempotency header.

    java
    @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 @RequestMapping without a method — accidental acceptance of DELETE.
    • Overloading one path with many params variants until nobody can read the API.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhy prefer @GetMapping over @RequestMapping for reads?+

    Answer

    It makes the HTTP method explicit and prevents accidentally handling unsafe methods on a read path.
    2IntermediateQuestionWhat do consumes and produces control?+

    Answer

    Which 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

    When a header is part of the contract (idempotency, API version). It still must be paired with service-level checks and authz.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.