Spring New @RequestMapping Annotations

Updated on August 14th, 2018

In this article, we will discuss composed annotation introduced in Spring release 4.3 to handle @RequestMapping Annotation in Spring MVC.

 

Introduction

Before Spring 4.3, We use @RequestMapping annotation to implement URL mapping.

@RequestMapping(value = "/hello", method = RequestMethod.GET)
 public String traditionalMapping(){
   return "hello";
}

With newly composed annotation, it’s easier and more readable.

@GetMapping(value = "/new-mapping")
 public String newMapping(){
    return "hello";
}

Here is the list of new @RequestMapping annotations introduced in Spring 4.3

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

These annotations naming convention clearly explain what each mapping suppose to handle.

  • @GetMapping – To handle GET request.
  • @PostMapping – For handling POST type request.

 

1. Setup

These annotations are part of the Spring MVC.If you are using Spring Boot for your project, we add the spring-boot-starter-web starter in pom.xml.

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

If you are starting with Spring Boot, read our Spring Boot guide.For traditional Spring MVC application, add spring-webmvc in pom.xml file.

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>5.0.4.RELEASE</version>
</dependency>

 

2. How It Works

These new annotations work by internally annotated with @RequestMapping.To understand it more clearly, let’s take a look at the source code of the @PostMapping annotation.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = RequestMethod.POST)
public @interface PostMapping {
// annotation body
}

As visible in the source code, @PostAnnotation internally using @RequestMapping, other annotations are also using the similar technique.Let’s take a look at these new annotations in more detail.

 

3. @GetMapping

@GetMapping(value = "/customers")
  public @ResponseBody ResponseEntity<List> getCustomers(){
     List customers = new ArrayList<>();
     return  new ResponseEntity<List>(customers,HttpStatus.OK);
 }

 @GetMapping(value = "/customers/{id}")
  public @ResponseBody ResponseEntity<Customer> getCustomer(@PathVariable String id){
     Customer customer = customerService.getCustomerById(id);
     return new ResponseEntity(customer,HttpStatus.OK);
 }

 

4. @PostMapping

@PostMapping(value = "/customers")
 public @ResponseBody ResponseEntity<Customer> getCustomers(Customer customer){
    Customer customer = customerService.createCustomer(customer);
    return new ResponseEntity(customer,HttpStatus.OK);
}

 

5. @PutMapping

@PutMapping(value = "/customers/{id}")
 public @ResponseBody ResponseEntity<Customer> getCustomers(Customer customer){
    Customer customer = customerService.updateCustomer(customer);
    return new ResponseEntity(customer,HttpStatus.OK);
}

 

6. @DeleteMapping

@DeleteMapping(value = "/customers/{id}")
 public @ResponseBody ResponseEntity<String> getCustomers(@PathVariable String id){
    customerService.deleteCustomerById(id);
    return new ResponseEntity("Customer Deleted",HttpStatus.OK);
}

 

7. @PatchMapping

@PatchMapping(value = "/customers/{id}")
 public @ResponseBody ResponseEntity<String> getCustomers(@PathVariable String id){
   return new ResponseEntity("Patch",HttpStatus.OK);
}

 

8. Testing

For testing these new annotations, we can create a simple REST application using Spring Boot or traditional Spring MVC.We can test our application 

  • Using traditional way by using the web browser.
  • Use any common REST client like PostMan, AdvanceRest client etc.

 

Summary

In this article, we discussed the new @RequestMapping introduced with Spring.We covered how to use these new annotations and benefits of using these as compared to the traditional request mapping available with Spring MVC.

Java Development Journal

Hello!! Welcome to the Java Development Journal. We love to share our knowledge with our readers and love to build a thriving community.

follow me on:

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of