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

With this blog post, I will share Spring annotations for mapping HTTP requests into specific handler methods. These are @PostMapping, @GetMapping, @PutMapping and the @DeleteMapping.

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

New Spring Boot REST Request Annotations

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

Below is a very simple Rest Controller class that uses this Spring Boot REST Requests annotations.

@RestController
@RequestMapping("users")
public class UserController {

 @Autowired
 UserService userService;
 
 @GetMapping("/status/check")
 public String status()
 {
  return "working";
 }
 
 @GetMapping("/{id}")
 public String getUser(@PathVariable String id)
 {
   

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

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

Request Annotations and their Attributes

Each of the above-mentioned annotations can accept a list of attributes which helps 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 an HTTP Get request is sent to /status/check, while the second one expects a variable value of {id} like, for example /17fhyr37 or /4hgj6j94jwg65

@GetMapping("/status/check")
public String status()
{
 return "Working";
}

@GetMapping("/{id}")
public String getUser(@PathVariable String id)
{
 return "HTTP Get was called";
}

Returning JSON or XML

You can specify which content type(JSON, XML) your Web Service endpoint can return 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 able to return more complex XML structures, add the following dependency to your pom.xml file:

<dependency>
 <groupId>com.fasterxml.jackson.dataformat</groupId>
 <artifactId>jackson-dataformat-xml</artifactId>
</dependency>

@RequestMapping Annotation

Additionally to the annotations mentioned above, you can use the @RequestMapping annotation, which you will probably find in most 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 CRUD operations.

HTTP GET Request

A few HTTP GET request mapping examples:

@RequestMapping(method=RequestMethod.GET)
@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})

HTTP POST request

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 to 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: 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

Leave a Reply

Your email address will not be published.