Spring MVC RESTful Web Service – difference between @Controller and @RestController

Creating RESTful web services is more comfortable and simpler when using Spring MVC Annotation.
The tutorial shows you overview of a traditional Spring MVC Controller and the Spring MVC RESTful Web Service Controller which are implemented in new Spring Framework, and difference between 2 annotations: @Controller and @RestController.

I. Introduction
1. Spring MVC

The Spring Web MVC uses a DispatcherServlet which handles all HTTP Requests and Responses.
This is lean workflow of Spring MVC Web:
DispatcherServlet uses HandlerMapping to know and call appropriate Controller.
– Appropriate Controller calls the right service methods and these methods will set model data based on defined business logic and returns View name to DispatcherServlet.
DispatcherServlet uses ViewResolver to choose appropriate View for the request.
– When View is finalized, DispatcherServlet passes the model data to the View.

2. RESTful Web Service Controller and Difference between @Controller – @RestControler

The main difference from RESTful Web Service Controller is the way the HTTP response body is created. RESTful Controller returns the object which data is written directly to HTTP response under JSON or XML without using View Resolver.
With new approach, we can return data directly from Controller without finding appropriate View by using annotation @ResponseBody over method.
@Controller is used to mark classes as Spring MVC Controller.
@RestController is the same as @Controller, but added @ResponseBody annotation.

II. Example Overview
1. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.0.RELEASE

2. Project Structure


3. Step by step

– Create Spring Boot project
– Create a DataModel
– Create Controllers
– Run Spring Boot Application & Enjoy Result

III. Practice
1. Create Spring Boot project

Open Spring Tool Suite, on Menu, choose File -> New -> Spring Starter Project, then fill each field:

Click Next, in Web: choose Web:

Click Finish. Spring Boot project will be created successfully.

2. Create a DataModel

Under package model, create Customer.java.

3. Create Controllers

Under package controller, create 2 controllers:



4. Run Spring Boot Application & Enjoy Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results:

Request 1

Request 2

IV. Source Code


By grokonez | September 19, 2016.

Last updated on June 4, 2017.

Related Posts

2 thoughts on “Spring MVC RESTful Web Service – difference between @Controller and @RestController”

  1. Thank you for the detailed description and flow diagrams! As an electronics engineer by degree I appreciate a good flow chart.


  2. Hi,

    How the client identifies the rest service and how we will communicate client to service.
    is there any certification requires to use the services?

    thanks & regards,

Got Something To Say:

Your email address will not be published. Required fields are marked *