Home/Learn/Spring Boot/Request Mapping & HTTP Methods

Request Mapping & HTTP Methods

Beginner
Web / REST

@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.

Java — HTTP verb shortcut annotations
@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.

Java — consumes, produces, headers
@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.

Java — wildcards, regex, matrix variables
// 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 Aria
1

What is the difference between @RequestMapping and @GetMapping?

EasyCognizant
2

How does the consumes attribute on @PostMapping work?

EasyWipro
3

How do you implement API versioning using request mappings?

MediumAmazon
4

What happens if two handler methods match the same request?

MediumGoogle
5

Explain how Spring resolves handler method ambiguity with produces and consumes.

HardNetflix

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.

Loading discussion…