Asked by: Crecencio Sturzebecher
asked in category: General Last Updated: 1st March, 2020

What is the use of @RequestBody?

@RequestBody and @ResponseBody annotations are used to bind the HTTP request/response body with a domain object in method parameter or return type. Behind the scenes, these annotation uses HTTP Message converters to convert the body of HTTP request/response to domain objects.

Click to see full answer.

In respect to this, what is difference between @RequestBody and @ResponseBody?

@RequestBody : Annotation indicating a method parameter should be bound to the body of the HTTP request. @ResponseBody annotation can be put on a method and indicates that the return type should be written straight to the HTTP response body (and not placed in a Model, or interpreted as a view name).

Subsequently, question is, is @ResponseBody required? You can see that if you use Spring MVC @Controller annotation to create a RESTful response you need to annotate each method with the @ResponseBody annotation, which is not required when you use @RestController. It not only makes your code more readable but also saves a couple of keystrokes for you.

Secondly, why we use @PostMapping?

@PostMapping to handle HTTP POST Requests Notice that the method responsible for handling HTTP POST requests needs to be annotated with @PostMapping annotation. Notice how the @RequestBody annotation is used to mark the method argument object into which the JSON document will be converted by Spring Framework.

What is @RequestBody in spring?

Simply put, the @RequestBody annotation maps the HttpRequest body to a transfer or domain object, enabling automatic deserialization of the inbound HttpRequest body onto a Java object. This is all that is needed for a Spring REST API and an Angular client using the @RequestBody annotation!

28 Related Question Answers Found

What is the use of @controller?

What is @RestController in spring boot?

What is @ResponseStatus?

CAN GET request have body?

What is @RestController?

What is @RequestMapping?

What is difference between @RequestBody and @ModelAttribute?

What is MVC in spring?

What is difference between RequestMapping and GetMapping?

What is @PostMapping in spring boot?

What is @GetMapping in spring boot?

What is @PostMapping?

What is @PathVariable?

What is the difference between RequestMapping and GetMapping in spring boot?