Request Mapping & HTTP Methods
Beginner@GetMapping, @PostMapping, @PutMapping, @DeleteMapping, and @PatchMapping are shortcuts for @RequestMapping(method=…) that map HTTP verbs to handler methods.
Overview
Spring MVC maps incoming HTTP requests to handler methods using annotations. @RequestMapping is the base annotation and accepts method, path, consumes, produces, headers, and params attributes. The shortcut annotations (@GetMapping, @PostMapping, etc.) provide cleaner code for the common HTTP verbs. Mappings can be applied at both the class level (base path) and method level (sub-path), and they support Ant-style wildcards and URI templates.
HTTP Method Shortcuts
Each HTTP verb has a dedicated annotation that delegates to @RequestMapping. Use these in preference to @RequestMapping(method=RequestMethod.GET) for readability.
@RestController
@RequestMapping("/api/orders") // base path for all methods below
public class OrderController {
@GetMapping // GET /api/orders
public List<OrderDTO> list() { ... }
@GetMapping("/{id}") // GET /api/orders/{id}
public OrderDTO get(@PathVariable Long id) { ... }
@PostMapping // POST /api/orders
@ResponseStatus(HttpStatus.CREATED)
public OrderDTO create(@RequestBody @Valid CreateOrderRequest req) { ... }
@PutMapping("/{id}") // PUT /api/orders/{id}
public OrderDTO replace(@PathVariable Long id, @RequestBody OrderDTO dto) { ... }
@PatchMapping("/{id}/status") // PATCH /api/orders/{id}/status
public OrderDTO updateStatus(@PathVariable Long id,
@RequestBody StatusRequest req) { ... }
@DeleteMapping("/{id}") // DELETE /api/orders/{id}
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable Long id) { ... }
}Consumes, Produces & Headers
Narrow mapping with consumes (Content-Type) and produces (Accept) to handle multiple representations of the same resource, or use headers/params for custom matching.
@PostMapping(
path = "/upload",
consumes = MediaType.MULTIPART_FORM_DATA_VALUE
)
public ResponseEntity<String> upload(@RequestParam MultipartFile file) { ... }
@GetMapping(
path = "/{id}",
produces = { MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_XML_VALUE }
)
public ProductDTO get(@PathVariable Long id) { ... }
// Header-based versioning
@GetMapping(path = "/{id}", headers = "API-Version=2")
public ProductV2DTO getV2(@PathVariable Long id) { ... }Wildcards & Matrix Variables
Ant-style wildcards (*, **) and {var} templates give flexible path matching. Matrix variables (;key=value in path segments) require enableMatrixVariables=true on the MVC config.
// Single-segment wildcard
@GetMapping("/files/*.txt")
public Resource getTextFile() { ... }
// Multi-segment wildcard
@GetMapping("/docs/**")
public Resource docs(HttpServletRequest req) { ... }
// Regex constraint on path variable
@GetMapping("/{id:[0-9]+}")
public ProductDTO getById(@PathVariable Long id) { ... }
// Matrix variable /products/42;colour=red;size=M
@GetMapping("/{id}")
public ProductDTO filter(
@PathVariable Long id,
@MatrixVariable String colour,
@MatrixVariable(required = false) String size) { ... }Key Points to Remember
- 1@GetMapping, @PostMapping, etc. are composed annotations for cleaner code than @RequestMapping(method=...).
- 2Class-level @RequestMapping sets a base path; method-level annotations append sub-paths.
- 3consumes narrows by Content-Type; produces narrows by Accept header.
- 4Path variables use {name} templates; regex constraints use {name:regex}.
- 5Ant wildcards: * matches one segment, ** matches multiple segments.
- 6Matrix variables (;key=val) need enableMatrixVariables = true in WebMvcConfigurer.
Interview Questions
Sign in to ask AriaWhat is the difference between @RequestMapping and @GetMapping?
How does the consumes attribute on @PostMapping work?
How do you implement API versioning using request mappings?
What happens if two handler methods match the same request?
Explain how Spring resolves handler method ambiguity with produces and consumes.
Ask Aria about Request Mapping & HTTP Methods
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.