Spring Framework 4.3 has introduced few convenient annotations for HTTP Request Mapping based on the HTTP Request Method.
Old Way | New Way |
@RequestMapping(value = “/test”, method = RequestMethod.GET) | @GetMapping(“/api/customers”) |
@RequestMapping(value = “/test”, method = RequestMethod.POST) | @PostMapping(“/api/create”) |
@RequestMapping(value = “/test”, method = RequestMethod.PUT) | @PutMapping(“/api/update”) |
@RequestMapping(value = “/test”, method = RequestMethod.DELETE) | @DeleteMapping(“/api/delete”) |
@RequestMapping(value = “/test”, method = RequestMethod.PATCH) | @PatchMapping(“/api/update”) |
Here is the Sample code
@RestController public class RestApiController { @GetMapping(value = "/user") public String getMappingExample() { return "HTTP GET : Request Mapping"; } @PostMapping(value = "/user") public String postMappingExample() { return "HTTP POST : Request Mapping"; } @PutMapping(value = "/user") public String putMappingExample() { return "HTTP PUT : Request Mapping"; } @DeleteMapping(value = "/user") public String deleteMappingExample() { return "HTTP DELETE : Request Mapping"; } }