@RequestMapping vs Spring composed annotations for HTTP Methods
@RequestMapping was introduced in Spring 2.5.
An example is listed below:
@RequestMapping(value = "/student/{studentId}/marks/determine",
method = RequestMethod.GET,
produces = { MediaType.APPLICATION_JSON_VALUE })The "method" in the HTTP request maps to the below RequestMethod:
GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.
From Spring 4.3 onwards, spring framework provides us with the below shortcuts for the respective HTTP method types.
Basically, we get rid of @RequestMapping and explicitly declaring the HTTP method type, the above code snippet becomes:
@GetMapping(value = "/student/{studentId}/marks/determine",
produces = { MediaType.APPLICATION_JSON_VALUE })
@GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET)
Other frequently used composed annotations are:
- @PostMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.POST)
- @PutMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.PUT)
- @DeleteMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.DELETE)