Spring @RequestBody annotation

In this article, we’ll be discussing the @RequestBody annotation in Spring MVC.
We’ll go over what it is, how to use it, and some of the pros and cons of doing so.

What is @RequestBody
@RequestBody is a Spring MVC annotation that is used over method arguments inside a Spring controller.
This annotation tells a controller that the object received in the body of the HTTP request is to be bound to a Java object.
When you use @RequestBody annotation on a method parameter, Spring will bind the incoming HTTP request body to that parameter.
If the parameter is a Java object, Spring will attempt to convert the body of the request to that object. This means that the request body is deserialized into an object.
Behind the scenes, spring uses Jackson ObjectMapper to perform this conversion.
Remember that @RequestBody will work with the methods that handle HTTP POST request. That is, they should be annotated with any of the below

@RequestMapping(value="save", method = RequestMethod.POST)

OR

@PostMapping("save")

How to use @RequestBody
To use @RequestBody, you simply need to annotate the method argument that you want to bind to the request body. In the following example, @RequestBody is applied over a string.
This means that when a string is sent in the request, it is automatically populated in the method argument annotated with @RequestBody.

@RestController
public class RequestBodyController {

  @PostMapping("/requestbody")
  public void printRequestBody(@RequestBody String requestBody) {
    System.out.println(requestBody);
  }
}

@RequestBody with object
When @RequestBody is used over an object, then its fields are populated with the corresponding fields of object in the request.
To understand it, consider below java class whose object will be used as the method parameter.

public class Employee {
   private Integer id;
   
   private String name;
  
   // getter and setter methods
}

and below is the controller method

@PostMapping("employee")
public void saveEmployee(@RequestBody Employee e) {
  // save employee to db
}

Now, when the above URL is accessed with below body

{
"id":1,
"name":"abc"
}

The fields of method parameter with @RequestBody is automatically populated with the values received in the request body.

Remember that the keys of json body must match the fields of the java object and their data type should be the same as their values.

Pros and Cons of @RequestBody
There are both pros and cons to using @RequestBody.

On the plus side, @RequestBody can be used to bind to a variety of different types of arguments. This can be helpful if you need to bind to a type that isn’t a standard Spring MVC type (like an HttpEntity).

On the downside, @RequestBody can sometimes be tricky to use. If you’re not careful, you can end up with errors that are difficult to debug.
Additionally, @RequestBody only works with request bodies that are in a format that Spring MVC can understand. This means that you can’t use @RequestBody with request bodies that are in a binary format (like images or videos).
Conclusion
In conclusion, @RequestBody is a useful annotation that can be used to bind a method argument to a request body.
Using this annotation, you can improve the quality of your code, apply validations over the java object to ensure that the incoming request contains mandatory fields and their values are in desired format etc., and build robust APIs.