Request Mapping

Master Spring MVC request mapping - how to map HTTP requests to controller methods using @RequestMapping, @GetMapping, @PostMapping, and the full range of mapping options.

Last reviewed: January 2025

·Spring Boot 2.7Spring Boot 3.x
✅
Quick Understanding: @RequestMapping in Spring Boot maps HTTP requests to controller methods using path, method, headers, params, consumes, and produces rules. @GetMapping, @PostMapping, and the other shortcuts make REST endpoints easier to read and maintain.

🔍 What Is It?

@RequestMapping in Spring Boot is the annotation-based routing system that tells Spring Boot which controller method should handle a specific HTTP request. When developers search for @RequestMapping in Spring Boot, they usually want to understand how path patterns, HTTP methods, and controller design work together.

Quick Facts

  • →Used in: REST controllers, web MVC endpoints, versioned APIs, and content-type specific handlers
  • →Best for: Building clear controller routes with explicit HTTP method behavior
  • →Interview importance: High because request mapping is a common Spring Boot interview topic
  • →Production relevance: High because API contracts, routing safety, and handler clarity depend on it
  • →Related topics: Controllers, path variables, request params, exception handling
  • →Last updated: July 2026

Mapping annotations

  • →@RequestMapping(path, method, consumes, produces, headers, params) - most flexible
  • →@GetMapping - shorthand for @RequestMapping(method=GET)
  • →@PostMapping - shorthand for @RequestMapping(method=POST)
  • →@PutMapping - shorthand for @RequestMapping(method=PUT)
  • →@DeleteMapping - shorthand for @RequestMapping(method=DELETE)
  • →@PatchMapping - shorthand for @RequestMapping(method=PATCH)

Mapping options

  • →Path: /users, /users/{id}, /users/**
  • →Method: GET, POST, PUT, DELETE, PATCH
  • →Consumes: application/json, multipart/form-data
  • →Produces: application/json, application/xml
  • →Headers: require specific request headers
  • →Params: require specific query parameters

🎯 Why Do We Need It?

Request mapping provides the routing contract for your Spring Boot API. Without it, there is no clear way to connect an HTTP request to the controller method that should validate input, call business logic, and shape the response.

Interview Relevance

  • →Interviewers ask request mapping to test whether you understand controller design, REST conventions, and handler selection
  • →Strong answers explain class-level versus method-level mappings, path variables, consumes or produces rules, and shortcut annotations
  • →Senior discussions often move into ambiguous mappings, API versioning, validation, and public contract stability

🏗️ What Happens in Real Projects?

Production Usage

Every REST API endpoint is defined by a combination of HTTP method + URL path:

  • →GET /api/v1/users - list users
  • →GET /api/v1/users/{id} - get user by ID
  • →POST /api/v1/users - create user
  • →PUT /api/v1/users/{id} - replace user
  • →PATCH /api/v1/users/{id} - partial update
  • →DELETE /api/v1/users/{id} - delete user

Production teams also use request mapping to control media types, route versioning, nested resources, and backward-compatible endpoint evolution across multiple clients.

⚙️ How It Works

Spring's DispatcherServlet receives every HTTP request. It consults the HandlerMapping built from your @RequestMapping annotations to find the matching controller method. If multiple methods match, Spring uses specificity rules to pick the best match. If no match is found, 404 is returned.

@RequestMapping vs @GetMapping

  • →@RequestMapping is the flexible base annotation for path, method, headers, params, consumes, and produces
  • →@GetMapping, @PostMapping, @PutMapping, and the others are shortcuts that make REST controller methods easier to scan quickly

💻 Example

Request Mapping Patterns — Common mapping patterns used in enterprise REST APIs.

java
@RestController
@RequestMapping("/api/v1/products")  // Base path for all methods
public class ProductController {

    @GetMapping                          // GET /api/v1/products
    public Page<ProductDto> list(@RequestParam(defaultValue="0") int page) { ... }

    @GetMapping("/{id}")                 // GET /api/v1/products/42
    public ProductDto getById(@PathVariable Long id) { ... }

    @PostMapping                         // POST /api/v1/products
    @ResponseStatus(HttpStatus.CREATED)
    public ProductDto create(@Valid @RequestBody CreateProductRequest req) { ... }

    @PutMapping("/{id}")                 // PUT /api/v1/products/42
    public ProductDto replace(@PathVariable Long id,
                               @Valid @RequestBody UpdateProductRequest req) { ... }

    @PatchMapping("/{id}")
    public ProductDto partialUpdate(@PathVariable Long id,
                                     @RequestBody Map<String, Object> updates) { ... }

    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void delete(@PathVariable Long id) { ... }

    // Nested resource: GET /api/v1/products/42/reviews
    @GetMapping("/{id}/reviews")
    public List<ReviewDto> getReviews(@PathVariable Long id) { ... }
}

🤔 Common Confusions

❓ What is the difference between @RequestMapping on class vs method?

@RequestMapping on the class defines the base path for all methods in that controller. Method-level annotations add to that base path. @RequestMapping("/api/v1/users") on the class + @GetMapping("/{id}") on a method = GET /api/v1/users/{id}.

❓ When should I use PUT vs PATCH?

PUT replaces the entire resource - the client sends the complete new state. PATCH partially updates - the client sends only the fields to change. In practice, many APIs use PUT for both. PATCH is more RESTful for partial updates but requires more careful implementation, especially around null versus missing fields.

🚨 Common Production Issues

Ambiguous mapping exceptions appear when two controller methods match the same URL and HTTP method combination.

Trailing slash mismatches can make /users and /users/ behave differently if clients and server configuration are not aligned.

Case-sensitive URL matching can cause avoidable 404 responses when public clients use inconsistent casing.

✅ Best Practices

Use specific method annotations such as @GetMapping instead of generic @RequestMapping whenever possible.

Put the base path on the class and the operation-specific route on the method.

Use lowercase, hyphenated URLs such as /user-profiles instead of /userProfiles.

Version public APIs early, for example with /api/v1/.

Be explicit with consumes and produces rules for non-JSON endpoints.

🏛️ Architect's Note

Senior Architect's Perspective

Consistent URL naming is an API contract. Once published, you cannot change endpoints casually without breaking clients. Establish naming conventions early: plural nouns for resources, lowercase with hyphens, and clear versioning from day one. Nested paths are useful, but keep them shallow enough that the resource model stays understandable.

💬 Frequently Asked Questions

How do I map multiple paths to one method?

Use an array such as @GetMapping({"/users", "/members"}) or @RequestMapping(path={"/v1/users", "/v2/users"}). This is useful for supporting aliases or controlled version transitions.

How does Spring handle trailing slashes?

In Spring Boot 3.x, trailing slash matching is disabled by default, so /users and /users/ are treated differently unless you configure path matching explicitly.

What is the difference between @RequestMapping and @GetMapping?

@RequestMapping is the general annotation that can define path, method, consumes, produces, headers, and params. @GetMapping is the shortcut used specifically for GET endpoints.

Why is request mapping important in Spring Boot interviews?

It is important because interviewers expect you to explain how REST endpoints are declared, matched, versioned, and kept maintainable in real APIs.

🎯 Key Takeaways

1@GetMapping, @PostMapping, and the other shortcuts are focused versions of @RequestMapping.
2Class-level @RequestMapping sets the base path and method annotations add operation-specific paths.
3Consumes and produces rules matter for content negotiation and API clarity.
4URL naming conventions should stay consistent across the whole API surface.
5Request mapping is both a coding topic and a public contract design topic.

🎯 Practice this topic in interviews

↗ Where this topic fits

Sources

Sources / References

Use these Spring Boot references to verify the concept in official docs and continue into the most relevant SplashRide pages.

Related SplashRide pages

SplashRide Spring Boot learning path

Browse the full Spring Boot topic collection and learning order on SplashRide.

Spring Boot Interview Prep

Practice Spring Boot interview questions and scenario-based answers.

Backend Engineer Career Path

A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.

Full Stack Java Developer Career Path

A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.

Controllers

Master Spring MVC Controllers — the web layer that handles HTTP requests, maps URLs to methods, and returns responses in enterprise REST APIs.

Spring @RequestParam

Learn how @RequestParam works in Spring Boot with query parameters, optional params, default values, list filters, form binding, and REST API examples.