@RequestMapping and @GetMapping are Spring MVC annotations used to map incoming HTTP requests to controller methods. The main difference is that @RequestMapping is a generic mapping annotation capable of handling multiple HTTP methods, whereas @GetMapping is a specialized shortcut annotation specifically designed for handling HTTP GET requests.
Key Points: • @RequestMapping can map GET, POST, PUT, DELETE, and other HTTP methods. • @GetMapping is dedicated only to HTTP GET requests. • @GetMapping improves code readability and reduces boilerplate code. • Internally, @GetMapping is a specialized form of @RequestMapping. • Modern Spring applications generally prefer method-specific annotations such as @GetMapping and @PostMapping.
What is @RequestMapping?
@RequestMapping is a general-purpose mapping annotation.
It can be applied at:
• Class level • Method level
Code Example:
@RestController
@RequestMapping("/employees")
public class EmployeeController {
@RequestMapping(value = "/all", method = RequestMethod.GET)
public String getEmployees() {
return "Employee List";
}
}In this example, the HTTP method must be specified explicitly.
Supported HTTP Methods
@RequestMapping can handle:
• GET • POST • PUT • DELETE • PATCH • OPTIONS • HEAD
Example:
@RequestMapping( value = "/save", method = RequestMethod.POST)
What is @GetMapping?
@GetMapping is a shortcut annotation introduced to simplify GET request mappings.
Code Example:
@RestController
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping("/all")
public String getEmployees() {
return "Employee List";
}
}This produces the same result as:
@RequestMapping( value = "/all", method = RequestMethod.GET)
but with cleaner syntax.
Internal Relationship
@GetMapping |
v
@RequestMapping(method = RequestMethod.GET )
Internally, Spring treats @GetMapping as a specialized version of @RequestMapping.
Comparison
@RequestMapping• Generic mapping annotation • Supports all HTTP methods • Requires explicit method declaration • More verbose
@GetMapping
• Specialized annotation • Supports only GET requests • No need to specify method • More readable and concise
Class-Level and Method-Level Usage
Code Example:
@RestController
@RequestMapping("/employees")
public class EmployeeController {
@GetMapping("/{id}")
public String getEmployee() {
return "Employee Details";
}
}Generated URL:
/employees/{id}
The class-level mapping acts as a common prefix.
Other Specialized Mapping Annotations
Spring provides additional shortcut annotations:
• @GetMapping • @PostMapping • @PutMapping • @DeleteMapping • @PatchMapping
Examples:
@PostMapping("/save")
@PutMapping("/update")
@DeleteMapping("/delete/{id}")These improve code readability and clearly indicate the intended HTTP operation.
Example: Suppose we expose an Employee API.
Using @RequestMapping:
@RequestMapping( value = "/employees", method = RequestMethod.GET)
Using @GetMapping:
@GetMapping("/employees")
Both handle GET requests, but @GetMapping is shorter and easier to understand.
Benefits of @GetMapping
• Cleaner code • Better readability • Less configuration • Easier maintenance • Clear REST API design
Real-World Example
In a RESTful Employee Management application:
Retrieve Employees
@GetMapping("/employees")
Create Employee
@PostMapping("/employees")
Update Employee
@PutMapping("/employees/{id}")
Delete Employee
@DeleteMapping("/employees/{id}")Using method-specific annotations makes the API self-explanatory and easier to maintain.
Interview Tip: A concise interview answer is:
"@RequestMapping is a generic annotation that can handle any HTTP method when the method attribute is specified. @GetMapping is a specialized shortcut for handling only HTTP GET requests. Internally, @GetMapping is equivalent to @RequestMapping(method = RequestMethod.GET), but it provides cleaner and more readable code."