Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping

Spread the love

In this article, we will discuss Spring 4.3. introduced HTTP method-specific shortcut variants of @RequestMapping. Spring RequestMapping new Shortcut Annotations are @GetMapping@PostMapping@PutMapping@DeleteMapping, and @PatchMapping.

New Annotations

Typically, if we want to implement the URL handler using traditional @RequestMapping annotation, it would have been something like this:

@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)

The new approach makes it possible to shorten this simply to:

@GetMapping("/get/{id}")

Spring currently supports five types of inbuilt shortcut annotations for handling different type of HTTP request.

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

From the naming convention, we can see that each annotation is meant to handle respective incoming request method type, i.e. @GetMapping is used to handle GET type of request method, @PostMappingis used to handle POST type of request method, etc.

How does this annotation work?

All of the above annotations are already internally annotated with @RequestMapping and the respective value in the method element.

For example, if we’ll look at the source code of @PostMapping annotation, we can see that it’s already annotated with RequestMethod.POST in the following way:

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

All the other annotations are created the same way. @GetMapping is annotated with RequestMethod.GET, @PutMapping is annotated with RequestMethod.PUT, etc.

The full source code of the annotations is available here.

Implementation

1. @GetMapping

@ResponseBody
    @GetMapping(value = "/employee/{id}")
    public Employee getEmployeeById(@PathVariable Long id)
    {

        return  employeeRepository.findById(id).get();
    }

2. @PostMapping

@PostMapping(value = "/employee")
    public Employee createEmployee(@RequestBody Employee employee)
    {
        return  employeeRepository.save(employee);
    }

3. @PutMapping

@PutMapping(value = "/employee")
    public Employee updateEmployee(@RequestBody Employee employee)
    {
        return  employeeRepository.existsById(employee.getId()) ? employeeRepository.save(employee) : null;
    }

4. @DeleteMapping

 @DeleteMapping(value = "/employee/{id}")
    public void deleteEmployee(@PathVariable Long id)
    {
         employeeRepository.deleteById(id);
    }

5. @PatchMapping

@PatchMapping("/patch")
public @ResponseBody ResponseEntity<String> patch() {
    return new ResponseEntity<String>("PATCH Response", HttpStatus.OK);
}

We have used the necessary annotations to handle proper incoming HTTP methods. @GetMapping to handle “/get” requests URI, @PostMapping to handle “/post” requests URI. and so on.

If we had to handle any URL path variable, we can simply do it in much less way we used to do in case of using @RequestMapping.

The code Example is available on Github

Click here to know more on Spring Framework.


“The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct.” 
― Donald E. Knuth, Selected Papers on Computer Science