There are some differences between Controller and RestController annotations. Let’s discuss them in detail.
MVC Pattern Vs Restful APIs
In developing applications in MVC pattern, Controller
annotation is used.
It provides functionalities like providing model to the handler method and resolves the view using ViewTemplate
.
But, In case of Restful APIs, we don’t need Model
and ViewTemplate
. In such cases, we should use RestController
which directly sends the returned object as a http response.
RequestBody annotation
When using the Controller annotation, if we want to send the returned object from the handler method as a http response, we should specify ResponseBody
annotation on the handler method.
But, in case of RestController
, we don’t need to specify the ResponseBody
annotation. Because, RestController
is a combination of Controller
and ResponseBody
annotations.
Thanks for reading