Spring MVC and Web Layer
August 14, 2021

M05 Q06 What is the difference between @RequestMapping and @GetMapping?

The main difference between @RequestMapping and @GetMapping is that the first one can be used to map any HTTP method requests and the second one can be used to map only HTTP GET method requests. @GetMapping is less flexible but easier to use

@GetMapping annotation is a composed annotation that is equal to @RequestMapping(method = RequestMethod.GET).

Both annotations allow you to specify multiple criteria for request mapping, like uri path, required headers, consumable media types, producible media types, however, only @RequestMapping allows you to specify HTTP method or HTTP methods through the method field. If none HTTP methods are specified, all HTTP methods will be mapped.

Spring also includes other specialized versions of @RequestMapping:

  • @PostMapping
  • @PutMapping
  • @DeleteMapping
  • @PatchMapping

Usage of those simpler, specialized versions is recommended for simple HTTP method mappings.