@ResponseBody @RequestBody and @RestController
@ResponseBody:
It tells Spring to use a message converter when sending data back to the client.
@RequestBody:
It tells Spring to use a message converter to convert a resource representation coming from a client to a respective object provided in the argument of the method.
Most probably developers use Jackson Libray for JSON to object and object to JSON conversion.
Code:
@RequestMapping(method=RequestMethod.POST
consumes="application/json")
public @ResponseBody
Spittle saveUser(@RequestBody User user) {
return userRepository.save(user);
}
@RestController
Spring 4.0 introduced the @RestController annotation to reduce the use of @ResponseBody and @RequestBody on each method
where the conversion is required.
Using @RestController Spring applies message conversion to all the methods in the controller class which is annotatted with the
@RestController annotation.
So, you does not require to annotate the methods with the @ResponseBody and @RequestBody annotations to expose the method as REST and to provide resources representation in different types.