1. Overview

This article is going to cover about using new composed annotations for @RequestMapping, the annotation for mapping web requests onto specific handler classes and/or handler methods in Spring Framework. Those annotations, which include @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping, have been introduced in the Spring Framework 4.3.

2. New Composed Annotations for @RequestMapping

Let’s see how the new composed annotations act as shortcuts for @RequestMapping annotation in the following situations:

Composed Annotations Shortcut For
@GetMapping @RequestMapping(method = RequestMethod.GET)
@PostMapping @RequestMapping(method = RequestMethod.POST)
@PutMapping @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping @RequestMapping(method = RequestMethod.DELETE)
@PatchMapping @RequestMapping(method = RequestMethod.PATCH)

2.1. @GetMapping Annotation

@GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET)

2.2. @PostMapping Annotation

@PostMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.POST)

2.3. @PutMapping Annotation

@PutMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.PUT)

2.4. @DeleteMapping Annotation

@PutMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.DELETE)

2.5. @PatchMapping Annotation

@PutMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.PATCH)

3. New Composed Annotations Examples

Let’s get though some examples about using new composed annotations for the @RequestMapping annotation.

3.1. Preparation

We’re going to use Spring Boot 1.5.2 in for these examples, and the Maven dependency is as following:

3.2. With @GetMapping Annotation

We use the HTTP GET method to retrieve resource information.

3.3. With @PostMapping

3.4. With @PutMapping

3.5. With @DeleteMapping

3.6. With @PatchMapping

4. Conclusion

The tutorial has just illustrated how to use new composed annotations for @RequestMapping annotation in Spring framework. They are provide us a more simple ways to define clearly mappings on our controllers.

The sample source code can be found on my Github project. It’s a Maven based project, so it’s easy to import into IDE likes Eclipse, Intellij, and so on.

 

 

0 0 vote
Article Rating