RESTSpring

Controller Vs RestController annotation in Spring

Posted On
Posted By admin

In this blog post, I will be explaining the Controller and RestController Spring annotations and also their differences.

MVC V/s REST

Before diving into the Controller and RestController annotations, it is important to understand the difference between a normal MVC web application and a REST application. An MVC Web application returns HTML which is rendered in a browser and displayed to end users.  A REST application, on the other hand, returns an XML or JSON response.

Controller Annotation

You can use the Controller annotation to designate a Controller class in an MVC application. Normally in such scenarios, the code maps each method in the controller to a URL specified via the RequestMappingannotation. When the UI requests a particular URL, the code invokes the appropriate method. This executes the business logic and then redirects the user to the appropriate JSP page.

Consider the following code:

@Controller
public class HelloWorldController {

  @RequestMapping("/hello")
  public ModelAndView helloWorld() {
 
    System.out.println("In HelloWorldController");
    String message = "<h1>Hello World</h1>";
    return new ModelAndView("hello", "message", message);
  }
}

 

This specifies that the class HelloWorldControlleris a Controller class. The code executes the method helloWorld

when the UI requests the URL /hello. It returns a ModelAndView object which specifies that the jsp with name “hello” should be displayed to the UI with the message specified.

Using Controller for REST service

You can also use the Controller annotation to create a REST service. As you probably know, a REST service returns am XML or JSON response, it does not return HTML. So when you use the Controller annotation for a REST service, you need to also specify the ResponseBodyannotation. This directly writes the values returned by the method to the Response stream.

Consider the following code snippet:

@Controller
@ResponseBody
public class HelloWorldController {

  @GetMapping("/hello")
  public String sayHello() {
    return "Hello World";
  }
}

The ResponseBodyannotation is specified along with the Controllerannotation. In this case, too, the code maps the URL /hello to the sayHellomethod. However, this method no longer returns a ModelAndViewobject and does not redirect the user to a JSP page. Instead, this method directly returns a String value. The ResponseBodyannotation converts the Java object returned by the method into the appropriate type (XML or JSON) based on the Content-Type specified in the HTTP request header and writes it to the Response stream.

Instead of using the ResponseBodyannotation on the Controller class, you can also use it along with each method in the Controller too.

RESTController annotation

As seen above, you can use the Controllerand ResponseBody annotations to create a REST service. Instead of using two annotations, Spring 4.0 introduced the RestController annotation. So the RestControllerannotation is a convenience controller which combines the behavior of the  Controllerand ResponseBodyannotations.

So the above code can be re-written as follows:

@RestController
public class HelloWorldController {

  @GetMapping("/hello")
  public String sayHello() {
    return "Hello World";
  }
  
}

This specifies that the class HelloWorldControlleris a Rest Controller. The code behaves exactly the way it would have if a Controllerand ResponseBodyannotation was used. So the String value returned by the method is converted into the appropriate type (XML or JSON) and written to the Response stream.

Differences

ControllerRestController
Normally used for an MVC controller, ut can be used for a REST service in conjunction with the ResponseBody annotationUsed for a RestController i.e. a Controller for a REST service
Methods in a Controller return a ModelAndViewMethods in a RestController directly return Java Objects which are written to the Response stream
Methods in a controller look for a view to display HTML to the UIMethods in a RestController directly write the Java objects into the Response stream which is converted to JSON or XML format
Present in Spring right from the startAdded in Spring 4.0

Further Learning

Spring MasterClass
Spring Tutorial For Beginners
Step by Step Spring MVC Tutorial
Spring Framework in Easy Steps

If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment