Posts

Difference between @RestController and @Controller

Image
The @RestController annotation in Spring MVC is nothing but a combination of the @Controller and the @ResponseBody annotation. It was added into Spring 4.0 to make the development of RESTful Web Services in Spring framework easier. If you are familiar with the REST web services you know that the fundamental difference between a web application and a REST API is that the response from a web application is a generally view of HTML + CSS + JavaScript while REST API just return data in form of JSON or XML. This difference is also obvious in the @Controller and the @RestController annotation. The job of the @Controller is to create a Map of model object and find a view but the @RestController simply returns the object and object data is directly written into HTTP response as JSON or XML. This can also be done with the traditional @Controller and the use of the @ResponseBody annotation but since this is the default behavior of RESTful Web services, Spring introduced @RestController which ...