Difference between @RestController and @Controller

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 combined the behavior of @Controller and @ResponseBody together.

1@Controller
2@ResponseBody
3public class MVCController { 
4  .. your logic
5}

1@RestController
2public class RestFulController { 
 3 .... your logic
4}

What are @Controller and @RestController in Spring?

In Spring framework, a Controller is a class which is responsible for preparing a model Map with data to be displayed by the view as well as choosing the right view itself. It can also directly write into response stream by using @ResponseBody annotation and complete the request.
The behavior of writing directly into response stream is very useful for responding calls to RESTful web services because there we just return data instead of returning a view.
If you have developed RESTful Web services before Spring 4 e.g. in Spring 3 or Spring 3.1, you would have been familiar by using a combination of the @Controller and the @ResponseBody to create a RESTful response. Spring guys take cognizant of this issues and created the @RestController.
Now, you don’t need to use the @Controller and the @RestponseBody annotation. Instead you can use the @RestController to provide the same functionality. In short, it is a convenience controller which combines the behavior of the @Controler and the @Response body into one.

Difference between @RestController and @Controller in Spring

Now that, you are familiar with both of these annotations, it’s a good time to analyze some factual difference between the @RestController and the @Controler
Anyway, let’s get back to the point, here are some important differences between these two annotations.
  1. The @Controller is a common annotation which is used to mark a class as Spring MVC Controller while the @RestController is a special controller used in RESTFul web services and the equivalent of @Controller + @ResponseBody.
  2. The @RestController is relatively new, added only on Spring 4.0 but @Controller is an old annotation, exists since Spring started supporting annotation, and officially it was added on Spring 2.5 version.
  3. The @Controller annotation indicates that the class is a “Controller” e.g. a web controller while the @RestController annotation indicates that the class is a controller where @RequestMapping methods assume @ResponseBody semantics by default i.e. servicing REST API.
  4. The @Controller is a specialization of @Component annotation while @RestController is a specialization of @Controller annotation. It is actually a convenience controller annotated with @Controller and @ResponseBody as shown below.
    1
    2
    3
    4
    5
    6
    @Target(value=TYPE)
    @Retention(value=RUNTIME)
    @Documented
    @Controller
    @ResponseBody
    public @interface RestController
    and here is how the declaration of @Controller looks like:
    1
    2
    3
    4
    5
    @Target(value=TYPE)
    @Retention(value=RUNTIME)
    @Documented
    @Component
    public @interface Controller
  5. One of the key difference between @Controler and @RestCotroller in Spring MVC is that once you mark a class as @RestController then every method is written a domain object instead of a view. 
  6. Another key difference between @RestController and @Controller is that you don’t need to use @ResponseBody on every handler method once you annotate the class with @RestController as shown below:
    with @RestControler:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    @RestController
    public class Book{
    @RequestMapping(value={"/book"})
    public Book getBook(){
    //...
    return book;
    }
    }
    without @RestController:
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    @Controller
    public class Book{
    @RequestMapping(value={"/book"})
    @ResponseBody
    public Book getBook(){
    //...
    return book;
    }
    }
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 key strokes for you.
Here is a simple HelloWorld example using @RestController and SpringBoot framework:
That’s all about the difference between @Controller and @RestController annotation in Spring MVC and REST. @RestController is nothing but the shortcut to use both @Controller and @ResponseBody annotation together.
Spring purposefully added this annotation in Spring 4 to make the development of RESTful web services easier using Spring framework. It can directly convert the response to JSON or XML depending upon MIME type of request.
So, if you are creating a RESTful Web Services it’s better to use @RestController than combining the @Controller to @ResponseBody.

Comments