What is the difference between @RequestMapping and its shortcut annotations (@GetMapping, @PostMapping, etc.)?
Quick Answer
@RequestMapping is the general-purpose, original mapping annotation, requiring an explicit method attribute (e.g., method = RequestMethod.GET) to restrict which HTTP method it handles, and can be applied at both the class level (a common path prefix) and method level. @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, and @PatchMapping are meta-annotations (introduced in Spring 4.3) that are each @RequestMapping pre-configured for one specific HTTP method, making method-level mappings more concise and their intent clearer at a glance.
Detailed Answer
@RequestMapping is the original, general-purpose mapping annotation — it can map a class or method to a URL path, and (without further attributes) matches any HTTP method unless you explicitly restrict it:
@RequestMapping(value = "/orders", method = RequestMethod.GET)
List<Order> listOrders() { ... }
@RequestMapping(value = "/orders", method = RequestMethod.POST)
Order createOrder(@RequestBody CreateOrderRequest request) { ... }
Since Spring 4.3, the shortcut annotations — @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, @PatchMapping — are provided as composed meta-annotations, each equivalent to @RequestMapping pre-configured for exactly one HTTP method:
@GetMapping("/orders")
List<Order> listOrders() { ... }
@PostMapping("/orders")
Order createOrder(@RequestBody CreateOrderRequest request) { ... }
Functionally identical to the @RequestMapping(method = ...) form, but more concise and — importantly — communicates the intended HTTP method at a glance, without needing to read the method attribute to know what verb a handler responds to.
Class-level @RequestMapping is still commonly used to declare a shared base path for all methods in a controller, combined with method-level shortcut annotations for the actual verbs:
@RestController
@RequestMapping("/api/orders") // shared prefix
class OrderController {
@GetMapping // GET /api/orders
List<Order> list() { ... }
@GetMapping("/{id}") // GET /api/orders/{id}
Order get(@PathVariable Long id) { ... }
@PostMapping // POST /api/orders
Order create(@RequestBody CreateOrderRequest req) { ... }
}
Rule of thumb: use the HTTP-method-specific shortcut annotations for individual handler methods (the modern, idiomatic default); reserve plain @RequestMapping for class-level base-path declarations, or the rare case where a single handler genuinely needs to respond to multiple HTTP methods at once.