@RequestMapping is the core Spring MVC annotation that maps web requests to handler methods in a controller. It can match on the URL path, HTTP method, query parameters, headers, and the Content-Type and Accept media types. Put it on a class to set a shared base path, and on methods to narrow the match.
For a single HTTP method, the shortcut annotations @GetMapping, @PostMapping, @PutMapping, @PatchMapping and @DeleteMapping are the usual choice. They are composed annotations that simply wrap @RequestMapping with a fixed method.
Spring combines the path on the class with the path on the method:
GET /api/products calls list(), and GET /api/products/5 calls get(5). The class-level mapping avoids repeating /api/products on every method. A mapping without a method attribute matches every HTTP method, which is rarely what you want, so state the method explicitly or use the shortcut annotations.
| Attribute | What it matches | Example |
|---|---|---|
value / path | URL path, including {variables} and patterns | path = "/orders/{id}" |
method | HTTP method | method = RequestMethod.POST |
params | Query or form parameters | params = "format=csv" |
headers | Request headers | headers = "X-Api-Client=mobile" |
consumes | Request Content-Type | consumes = "application/json" |
produces | Response media type, matched against Accept | produces = "application/json" |
When several attributes are given, all of them must match. A request that matches the path but not consumes receives 415 Unsupported Media Type, and one that fails produces receives 406 Not Acceptable. A path that matches but with the wrong method gives 405 Method Not Allowed.
The classic form and the modern shortcuts are equivalent:
@PostMapping(consumes = ...) is exactly @RequestMapping(method = RequestMethod.POST, consumes = ...), with less code. Type-level produces applies to every method in the class unless a method overrides it.
@RequestMapping paths support URI template variables and patterns:
GET /users/3/orders/9 fills both path variables. The params = "q" condition means /search only matches when a q parameter is present. Since Spring Framework 6, trailing-slash matching is off by default, so /api/products/ is no longer treated as /api/products; register the exact path your clients call.
@RequestMapping on the class for the common prefix and shared produces or consumes.@GetMapping and friends on methods, because the intent is clear at a glance.@RequestMapping on a method only when one handler must serve several HTTP methods, for example method = {RequestMethod.GET, RequestMethod.HEAD}./api/orders/7) rather than verb-oriented (/api/getOrder).Recent Spring Framework releases also add first-class API versioning to mappings, through a version attribute on the mapping annotations. Check the reference documentation of the Spring version you use before relying on it.
405 on a path that exists: the mapping has the wrong method, or the client sent a different verb.404 with a trailing slash: see the trailing-slash change above./.No. It remains fully supported and is the base of all the shortcut annotations. The shortcuts are simply preferred for readability when a method handles one HTTP verb.
@GetMapping is shorthand for @RequestMapping(method = RequestMethod.GET). Behaviour is identical, with less code.
Yes. Pass an array, for example @GetMapping({"/items", "/products"}).
There is none. Without a method attribute the mapping matches all HTTP methods, so specify it or use a shortcut annotation.
@RequestMapping maps requests to controller methods by path, HTTP method, parameters, headers and media types. Use it on the class for shared settings, prefer @GetMapping, @PostMapping and the other shortcuts on methods, and use consumes and produces to declare the content types your endpoints accept and return.