What is @RequestMapping and what are its HTTP-method shortcut annotations?
Learn how @RequestMapping routes requests in Spring Boot and how @GetMapping, @PostMapping, @PutMapping and @DeleteMapping shortcuts keep controllers clean.
Expected Interview Answer
@RequestMapping is a Spring annotation that maps incoming HTTP requests to controller classes and handler methods based on URL path, HTTP method, headers, params, and content type.
Placed on a class it defines a base path, and on a method it defines the specific route. Because most handlers target one verb, Spring 4.3 added shortcut annotations — @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, and @PatchMapping — that are simply @RequestMapping preconfigured with a fixed method. They make controllers shorter and their intent clearer.
- Centralizes routing configuration on the controller
- Shortcuts make the HTTP verb explicit and readable
- Supports path variables, params, headers, and content negotiation
- Class-level base path avoids repeating URL prefixes
- Reduces boilerplate compared to the verbose method attribute
AI Mentor Explanation
A team's fielding plan assigns each fielder a zone so every ball hit there is handled by the right person. @RequestMapping is the plan mapping a delivery to a fielder, and @GetMapping or @PostMapping is like pre-stationing a specialist slip or a boundary rider for one exact shot.
Step-by-Step Explanation
Step 1
Annotate the controller
Put @RestController on the class and an optional class-level @RequestMapping to set a base path like /api/users.
Step 2
Choose the verb
Pick the shortcut matching the operation: @GetMapping to read, @PostMapping to create, @PutMapping to replace, @DeleteMapping to remove.
Step 3
Define the path
Give the shortcut a path segment, e.g. @GetMapping("/{id}") which combines with the base path.
Step 4
Bind inputs
Add @PathVariable, @RequestParam, or @RequestBody parameters so Spring extracts request data into method arguments.
Step 5
Return a response
Return a domain object or ResponseEntity; Spring serializes it and applies the correct status.
What Interviewer Expects
- Knows @RequestMapping works at class and method level
- Can name the five verb shortcuts and their target methods
- Understands shortcuts are meta-annotations over @RequestMapping
- Aware of extra attributes like params, headers, produces, consumes
- Can explain how class and method paths combine
Common Mistakes
- Thinking @RequestMapping only works at method level
- Using @RequestMapping with method=GET when @GetMapping is cleaner
- Forgetting the class-level base path when reasoning about the final URL
- Confusing @PutMapping (full replace) with @PatchMapping (partial update)
- Assuming shortcuts add behavior beyond fixing the HTTP method
Best Answer (HR Friendly)
“@RequestMapping is how Spring Boot decides which piece of code should answer a given web request based on its address and type. The shortcuts like @GetMapping and @PostMapping are quick ways of saying the request is a read or a create, which keeps the code short and easy to read.”
Code Example
@RestController
@RequestMapping("/api/users")
public class UserController {
@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
return userService.findById(id);
}
@PostMapping
public User createUser(@RequestBody User user) {
return userService.save(user);
}
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable Long id) {
userService.delete(id);
}
}Follow-up Questions
- How do class-level and method-level paths combine into the final URL?
- What is the difference between @PutMapping and @PatchMapping?
- How can you restrict a mapping by request parameter or header?
- What do the produces and consumes attributes control?
- Why were the verb shortcut annotations introduced in Spring 4.3?
MCQ Practice
1. Which annotation is equivalent to @RequestMapping(method = RequestMethod.POST)?
@PostMapping is a composed annotation that presets the HTTP method to POST.
2. Where can @RequestMapping be applied?
At class level it sets a base path; at method level it defines the specific route.
3. Which mapping typically represents a partial update of a resource?
@PatchMapping maps HTTP PATCH, used for partial updates, while @PutMapping replaces the whole resource.
Flash Cards
What is @RequestMapping? — An annotation that maps HTTP requests to controller classes and handler methods by path, method, params, and headers.
Name the five verb shortcuts. — @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, @PatchMapping.
What does a class-level @RequestMapping do? — Defines a base path that is prepended to every method-level route in that controller.
Are shortcuts different from @RequestMapping? — No — they are meta-annotations of @RequestMapping with the HTTP method pinned to one verb.
What does consumes control? — The Content-Type a handler accepts, restricting which requests it will process.
Continue Learning
Related Interview Questions
What is the difference between @RequestParam, @PathVariable, and @RequestBody?
medium
How does validation work in Spring Boot with @Valid and Bean Validation?
medium
What is Spring Boot and how does it differ from the Spring Framework?
easy
What is auto-configuration in Spring Boot and how does it work?
medium