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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> |
3.2. With @GetMapping Annotation
We use the HTTP GET method to retrieve resource information.
1 2 3 4 |
@GetMapping(value = "/books") public ResponseEntity<Iterable<Book>> getAllBooks() { return new ResponseEntity<>(bookRepository.findAll(), HttpStatus.OK); } |
3.3. With @PostMapping
1 2 3 4 5 |
@PostMapping(value = "/books") public ResponseEntity<Book> createBook(@RequestBody Book book) { bookRepository.save(book); return new ResponseEntity<>(book, HttpStatus.CREATED); } |
3.4. With @PutMapping
1 2 3 4 5 6 7 8 9 10 11 |
@PutMapping(value = "/books/{id}") public ResponseEntity<Book> updateBook(@PathVariable("id") Long id, @RequestBody Book Book) { Book dbBook = bookRepository.findOne(id); if (dbBook == null) { return new ResponseEntity<Book>(HttpStatus.NOT_FOUND); } dbBook.setName(Book.getName()); dbBook.setAuthor(Book.getAuthor()); return new ResponseEntity<Book>(dbBook, HttpStatus.OK); } |
3.5. With @DeleteMapping
1 2 3 4 5 6 7 8 9 |
@DeleteMapping(value = "/books/{id}") public ResponseEntity<Book> deleteBook(@PathVariable("id") Long id) { Book book = bookRepository.findOne(id); if (book != null) { return new ResponseEntity<Book>(HttpStatus.NOT_FOUND); } return new ResponseEntity<Book>(HttpStatus.NO_CONTENT); } |
3.6. With @PatchMapping
1 2 3 4 5 6 7 8 9 10 11 |
@PatchMapping(value = "/books/{id}") public ResponseEntity<Book> updatePartially(@PathVariable("id") Long id, @RequestBody Book Book) { Book dbBook = bookRepository.findOne(id); if (dbBook == null) { return new ResponseEntity<Book>(HttpStatus.NOT_FOUND); } dbBook.setName(Book.getName()); dbBook.setAuthor(Book.getAuthor()); return new ResponseEntity<Book>(dbBook, HttpStatus.OK); } |
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.