π 1. @RequestMapping
β
General-purpose annotation to map any HTTP method (GET, POST, etc.)
π Syntax:
π Key Features:
- Can handle any HTTP verb
- Supports path, method, params, headers, consumes, produces
- More verbose but flexible
π 2. @GetMapping
β
A specialized shortcut for @RequestMapping(method = RequestMethod.GET)
π Syntax:
π Key Features:
- Introduced in Spring 4.3
- Less verbose, cleaner, and more readable
- Only supports GET requests
π Side-by-Side Comparison
Feature |
@RequestMapping |
@GetMapping |
HTTP Methods Supported |
All (GET , POST , PUT , DELETE , etc.) |
Only GET
|
Verbosity |
More verbose |
Concise |
Flexibility |
Highly flexible |
Use only for GET |
Common Use Case |
Complex mappings, RESTful APIs |
Simple GET endpoints |
Introduced In |
Early Spring versions |
Spring 4.3+ |
β
Other Specialized Annotations
Annotation |
Purpose |
@GetMapping |
For GET requests |
@PostMapping |
For POST requests |
@PutMapping |
For PUT requests |
@DeleteMapping |
For DELETE requests |
@PatchMapping |
For PATCH requests |
π― Recommendation
β
Use @GetMapping
, @PostMapping
, etc. for cleaner code
β
Use @RequestMapping
when you need multiple methods or complex conditions