Asked by: Crecencio Sturzebecherasked in category: General Last Updated: 1st March, 2020
What is the use of @RequestBody?
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!