Enterprise Java

Difference between @RestController and @Controller Annotation in Spring MVC and REST

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.

In short, the following two code snippets are equal in Spring MVC:

@Controller
@ResponseBody
public class MVCController { 
   .. your logic
}
@RestController
public class RestFulController { 
  .... your logic
}

Obviously, everybody would like to declare just one annotation instead of two. Also, the @RestController is more obvious than the previous two.

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 as explained in my earlier post about how Spring MVC works internally.

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.

You can further join Eugen Paraschiv’s REST with Spring Master class if you are more curious about learning the advanced techniques to develop RESTFul Web Service in Spring.

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. This is a very important concept, not just from Interview point of view but also from Spring Core and Spring Web Application developer Certification. If you are preparing for Spring certifications, you should be familiar with such subtle differences. Additionally, you can also take a look at free Spring tests to get an idea about exam format and level of questions.

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.
     
    @Target(value=TYPE)
    @Retention(value=RUNTIME)
    @Documented
    @Controller 
    @ResponseBody
    public @interface RestController
    

    and here is how the declaration of @Controller looks like:

     
    @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. You can see Bryan Hassen’s Introduction to Spring MVC 4 to learn more about how to use the @RestController annotation in your Spring based application.
  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:

    @RestController
    public class Book{
    
    @RequestMapping(value={"/book"})
    public Book getBook(){
    //...
    return book;
    }
    }

    without @RestController:

    @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.

If you want to learn more about developing RESTful Web Services using Spring and Spring Security framework, I suggest you join Eugen Paraschiv’s REST with Spring Coaching class. Eugen has some good real world experience in developing and securing RESTful web services in Java and this class is a good opportunity to benefit from his immense experience.

Javin Paul

I have been working in Java, FIX Tutorial and Tibco RV messaging technology from past 7 years. I am interested in writing and meeting people, reading and learning about new subjects.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
hamed
7 years ago

nice explanation. very helpful for beginner like me. thanks . . .

Andrey
Andrey
5 years ago

On the other hand @Controller is more versatile. Its methods can return both view names (normally) or complex objects (if the method extra annotated)

raghu
raghu
5 years ago

great job nice explanation..

Andrew
1 year ago

Awesome explanation on rest controller vs spring controller.

Back to top button