@RequestMapping maps incoming web requests to controller classes and handler methods. It matches on the URL path and can also narrow the match by HTTP method, query parameters, headers, and the Content-Type and Accept media types. Every other mapping annotation in Spring MVC, such as @GetMapping and @PostMapping, is a shortcut built on top of it.
It lives in org.springframework.web.bind.annotation and is used in both Spring MVC and Spring Boot applications. Internally, RequestMappingHandlerMapping scans controller beans at startup, builds a registry of mappings and uses it to find the handler for each request.
@RequestMapping is the central routing annotation of Spring MVC. You can put it on a class to define a shared base path, and on methods to define specific endpoints. The two paths are combined.
A request to /api/products/ping reaches ping(). Without a method attribute the mapping matches every HTTP method, which is rarely what you want for an API.
| Attribute | Purpose | Example |
|---|---|---|
value / path | URL pattern(s) to match | path = "/orders/{id}" |
method | Allowed HTTP methods | method = RequestMethod.POST |
params | Required query or form parameters | params = "debug=true" |
headers | Required request headers | headers = "X-Client=mobile" |
consumes | Accepted request Content-Type | consumes = "application/json" |
produces | Media types the method can return | produces = "application/json" |
name | Name for building links | name = "orderDetail" |
When several conditions are given, a request must satisfy all of them. If no handler matches, Spring responds with 404 for an unknown path, 405 when the path exists but the method does not, and 415 or 406 when consumes or produces do not match.
The class-level path is a prefix. Each method then adds its own path, HTTP method and media types.
The class-level produces applies to every method unless a method overrides it.
Since Spring 4.3 you can use composed annotations instead of setting method yourself. They are shorter, clearer and are the recommended style for new code:
| Shortcut | Equivalent |
|---|---|
@GetMapping | @RequestMapping(method = RequestMethod.GET) |
@PostMapping | @RequestMapping(method = RequestMethod.POST) |
@PutMapping | @RequestMapping(method = RequestMethod.PUT) |
@PatchMapping | @RequestMapping(method = RequestMethod.PATCH) |
@DeleteMapping | @RequestMapping(method = RequestMethod.DELETE) |
A common modern pattern is @RequestMapping on the class for the shared prefix and the shortcut annotations on methods:
Path templates capture parts of the URL, and params or headers let you pick between handlers for the same path:
Spring MVC uses PathPattern matching by default (the default since Spring Framework 6), where ** is only allowed at the end of a pattern. The trailing slash is no longer matched automatically since Spring Framework 6, so /products and /products/ are different URLs unless you register both.
@GetMapping, @PostMapping and the other shortcuts over @RequestMapping(method = ...).DELETE.@RestController for APIs. Use @Controller plus view names for server-rendered pages./api/orders/{id}.consumes and produces to make content negotiation explicit.IllegalStateException at startup.No. It remains fully supported and is still the right choice for class-level prefixes and for combining unusual conditions. For single methods, the shortcut annotations are usually nicer.
@GetMapping is a composed annotation equal to @RequestMapping(method = RequestMethod.GET). Behaviour is identical, only the notation is shorter.
Yes. Pass an array: @GetMapping({"/a", "/b"}). Both paths reach the same method.
The mapping matches every HTTP method. Prefer an explicit method to avoid exposing handlers to verbs you never intended.
@RequestMapping is the base routing annotation in Spring MVC. It matches on path, method, parameters, headers and media types, works on classes and methods, and powers the @GetMapping family. Use it for shared prefixes, use the shortcuts for individual endpoints, and always limit the HTTP method.