Spring Boot – @PostMapping, @GetMapping, @PutMapping, @DeleteMapping

With this blog post, I am going to share with you Spring annotations for mapping HTTP requests into specific handler methods. Then @PostMapping, @GetMapping, @PutMapping and the @DeleteMapping.

In most of the Spring Boot applications, you will see the use of @RequestMapping annotation, which I am also going to include in this blog post. But I will start with the new ones which are more like shortcut annotations and are available since Spring 4.3

New Spring Boot REST Request Annotations

  • @PostMapping – Handle HTTP POST Requests
  • @GetMapping – Handle HTTP Get Requests
  • @PutMapping – Handle HTTP Put Requests
  • @DeleteMapping – Handle HTTP Delete Requests

Below is a very simple RestController which makes use of this Spring Boot REST Requests annotations.

public class UserController {

 UserService userService;
 public String status()
  return "working";
 public String getUser(@PathVariable String id)

  return "HTTP Get was called";
 public String createUser(@RequestBody UserDetailsRequestModel requestUserDetails)

  return "HTTP POST was called";
 public String deleteUser(@PathVariable String userId)
  return "HTTP DELETE was called";
 public String updateUser(@PathVariable String userId, @RequestBody UserDetailsRequestModel requestUserDetails)
  return "HTTP PUT was called";

I hope this short reference to Spring Boot Annotations for mapping HTTP Requests to specific methods is helpful to you. Have a look at the below video courses which might help you to learn how to build RESTful Web Services and take your skills to a new level!

Request Annotations and their Attributes

Each of the above-mentioned Spring Boot REST annotations can accept a list of attributes which help to make the binding of HTTP Request and a Java method more specific.

For example, you might have two methods that handle HTTP GET Requests but they will be available at a different URL path.

Binding a Method to a Specific URL Path

Have a look at the below two methods that are annotated with @GetMapping. Each @GetMapping annotation specifies a different URL path. The first method will be triggered when HTTP Get request is sent to /status/check while the second one expects a variable value of {id} like for examples /17fhyr37 or /4hgj6j94jwg65

public String status()
 return "Working";

public String getUser(@PathVariable String id)
 return "HTTP Get was called";

Returning JSON or XML 

You can specify which content type your Web Service endpoint can return by using the produces attribute with the request annotation.

@GetMapping(path = "/{id}", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
public UserModel getUser(@PathVariable String id)
   // other code here to create UserModel object

 return userModel;

and to make your Spring application be able to return a bit more complex XML structures add the following dependency to your pom.xml file:


@RequestMapping Annotation

Additionally to the above-mentioned shortcut annotations, you can use the @RequestMapping annotation which you will most probably find in most of the Spring Boot REST Web Service projects. You can use this single @RequestMapping annotation to map any request to a specific method. Below is an example of using @RequestMapping annotation for each of the CRUD operations.

A few HTTP GET request mapping examples:

@RequestMapping(method=RequestMethod.GET, path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
@RequestMapping(method=RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
@RequestMapping(method=RequestMethod.GET, path = "/{id}", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})

To map HTTP Post request to a method, we just need to change the Request Method type:

@RequestMapping(method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@RequestMapping(method=RequestMethod.POST, path = "/{id}", consumes=MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)

Please note that if you need your Web Service endpoint to be able to return information in either XML or JSON format. Depending on the value of the Accept Header. Then you need you use the curly brackets {} and list the supported media types like so:

@RequestMapping(method=RequestMethod.POST, path = "/{id}", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})

Note: Note that if you want your Web Service endpoint to return MediaType.APPLICATION_XML_VALUE then your HTTP Request must contain the Accept header with a value application/xml like in the curl example below:

curl -X GET \
  http://localhost:8080/users/Ow8EG5GAvkCa9kEHGqLTQpR32zSmhd \
  -H 'accept: application/xml' \
  -H 'authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0ZXN0QHRlc3QuY29tIiwiZXhwIjoxNTIzMDMwNTg4fQ.rnOqIEpmh6xQYBPkcRHp9DOnT8M5a7o9De6a7ZE7z2nBKfNpgXNrXgFQnlUi01CnltOP-iXyprsGhB4wvkafcg'

Video tutorials

Learning How to Build RESTful Web Services

Leave a Reply

Your email address will not be published. Required fields are marked *