@RequestMapping in Spring MVC — URL Mapping and HTTP Methods
Fast recall
Quick revision
Core answer
Basic understanding of Spring MVC request routing and URL mapping annotations
Using verbose @RequestMapping with method parameter instead of shorthand annotations like @GetMapping
Explain how class-level and method-level mappings combine, and show awareness of composed annotations
Interview-ready guide
Interview answer
@RequestMapping is how you tell Spring which controller method should handle a specific HTTP request.
You can use it at the class level to set a base path for all your endpoints.
For example, if you put @RequestMapping with "/api/orders" on the class, that becomes the prefix for every method inside. Then at the method level, you add sub-paths and specify the HTTP method. But here's the thing — nobody uses @RequestMapping with the method parameter anymore.
Spring gives you shorthand annotations: @GetMapping, @PostMapping, @PutMapping, @DeleteMapping. These are way cleaner and they're actually composed annotations that wrap @RequestMapping internally. So @GetMapping is just shorthand for @RequestMapping with method equals GET.
In practice, you'd have @RequestMapping on the class with your base path, then @GetMapping, @PostMapping on individual methods. Spring combines them — if your class has "/api/orders" and your method has "/{id}", the full path is "/api/orders/{id}". You extract that id parameter using @PathVariable in your method signature.
A common mistake is writing the old verbose style — @RequestMapping with value and method parameters on every method. That's outdated Spring 2 style. Modern Spring Boot uses the shorthand annotations.
Another mistake is not using class-level mapping and repeating the full path in every method. Then if you need to change the base path, you're editing ten methods instead of one line.
@RequestMapping in Spring MVC — URL Mapping and HTTP Methods
Direct Answer
@RequestMapping maps HTTP requests to controller methods or classes. At the class level it sets the base URL path; at the method level it refines with sub-path and HTTP method. Shorthand annotations @GetMapping, @PostMapping, etc., are composites of @RequestMapping.
Code Example
@RestController
@RequestMapping("/api/customers") // Class-level base path
public class CustomerController {
private final CustomerService customerService;
@GetMapping // Maps GET /api/customers
public List<CustomerResponse> getAllCustomers() {
return customerService.findAll().stream()
.map(CustomerResponse::new)
.toList();
}
@GetMapping("/{id}") // Maps GET /api/customers/{id}
public ResponseEntity<CustomerResponse> getCustomer(@PathVariable Long id) {
var customer = customerService.findById(id);
return ResponseEntity.ok(new CustomerResponse(customer));
}
@PostMapping // Maps POST /api/customers
public ResponseEntity<CustomerResponse> createCustomer(@RequestBody CustomerRequest request) {
var customer = customerService.create(request.toCustomer());
return ResponseEntity.status(HttpStatus.CREATED)
.body(new CustomerResponse(customer));
}
@DeleteMapping("/{id}") // Maps DELETE /api/customers/{id}
public ResponseEntity<Void> deleteCustomer(@PathVariable Long id) {
customerService.delete(id);
return ResponseEntity.noContent().build();
}
}
Common Mistakes & Pitfalls
- Verbose Syntax: Writing @RequestMapping(value="/users", method=RequestMethod.GET) instead of @GetMapping("/users"). The shorthand is standard in modern Spring Boot and more readable.
- Missing Class-Level Path: Repeating the full path in every method — @GetMapping("/api/users") in ten methods instead of @RequestMapping("/api/users") once at class level. This makes refactoring paths painful.
- Forgetting Path Variables: Declaring a path template like "/{id}" but not extracting it with @PathVariable in the method signature. Spring can't match the request because the parameter isn't bound.
- Ambiguous Mappings: Creating two methods with the same path and HTTP method — Spring throws an error at startup because it can't decide which method to call.
Check your understanding
Answer these without looking back at the article.
- What is the difference between @PutMapping and @PatchMapping?
- What does produces and consumes do in @RequestMapping?
- Can a single method handle multiple HTTP methods?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.