<div dir="ltr" style="text-align: justify;" trbidi="on">The key difference between a traditional Spring MVC @Controller and the RESTful web service @RestController is the way the HTTP response body is created. While the traditional MVC controller relies on the View technology, the RESTful web service controller simply returns the object and the object data is written directly to the HTTP response as JSON/XML by registered HttpMessageConverters.</p>
<div align="center" id="ads-id"></div>
<p><b>Traditional Spring MVC Workflow</b><br />
The following image describe a traditional Spring MVC Request workflow:</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="http://50.87.249.200/~dineshon/wp-content/uploads/2017/04/Spring-MVC-traditional-workflow.png" /></div>
<p><b><i>@ImageSource-https://www.genuitec.com</i></b></p>
<ul style="text-align: left;">
<li><b>Step 1:</b> The browser or client sends a request to the web application.</li>
<li><b>Step 2: </b>The request lands to the dispachter servlet and consult with handler mapping.</li>
<li><b>Step 3:</b> The Handler mappings defined in the application context file, this has information about which is controller need to be invoked.</li>
<li><b>Step 4:</b> The controller will be invoked and it can request the model for some information using DAOs and Services.</li>
<li><b>Step 5:</b> After Requests are processed by the Controller and the response is returned to the DispatcherServlet with logical view name</li>
<li><b>Step 6:</b> Then return to the view resolver prepared the view based on your configuration decide the which configuration (JSP, Velocity, PDF etc.) to be invoked.</li>
</ul>
<p>
<b>For Example:</b></p>
<pre class="highlight">@Controller
public class AccountController {
 @RequestMapping(value="/account", method=RequestMethod.GET)
 public ModelAndView get(Long accountId) {
 //Process model object
 return new ModelAndView("account-detail", model) ;
 }
}
</pre>
<div align="left" id="ads-id-id"></div>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;"><b>Popular Tutorials</b></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/2012/06/spring-30-baby-step-to-learn.html"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-web-mvc-framework-chapter-38.html"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/2016/06/introduction-to-spring-boot-a-spring-boot-complete-guide.html"><em><strong>Spring Boot Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/02/spring-security-take-baby-step-to-secure.html"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint.html"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/using-spring-jdbc-framework-chapter-32.html"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/spring-hateoas-hypermedia-driven-restful-web-service.html"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/microservices-with-spring-boot.html"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/06/jax-rs-web-service-tutorial.html"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/01/core-java-baby-step-to-be-best-java-ian.html"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/03/hibernate-3-on-baby-steps.html"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-batch-process-with-example.html"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p>
<b>Spring MVC REST Workflow</b><br />
The following image describe a Spring MVC REST workflow:</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="http://50.87.249.200/~dineshon/wp-content/uploads/2017/04/Spring-REST-workflow.png" /></div>
<p><b><i>@ImageSource-https://www.genuitec.com</i></b></p>
<ul style="text-align: left;">
<li><b>Step 1: </b>The client sends a request to a web service in URI form.</li>
<li><b>Step 2:</b> The request lands to the dispachter servlet and consult with handler mapping.</li>
<li><b>Step 3:</b> The Handler mappings defined in the application context file, this has information about which is controller need to be invoked.</li>
<li><b>Step 4: </b>The controller will be invoked and process model.</li>
<li><b>Step 5:</b> Requests are processed by the Controller and the response is returned to the DispatcherServlet which then dispatches to the view.</li>
</ul>
<p>
In Traditional Spring MVC Workflow, notice that controller return the ModelAndView object with logical view name to dispactcher servlet, it is forwarded to the ViewResolver in step 6. But in the Spring MVC REST Workflow, Spring lets you return data directly from the controller, without looking for a view, using the @ResponseBody annotation on a method. As of Spring Version 4.0, this process has been more simplified by the @RestController annotation.</p>
<p><b>For Example:</b></p>
<p><b>Using the @ResponseBody Annotation</b><br />
If any handler method of controller class is annotated with @ResponseBody, Spring converts the return value of this method and writes it to the http response automatically. </p>
<p><b>What is Behind the @ResponseBody</b><br />
There is a list of HttpMessageConverters registered in the background when we enable Spring web mvc feature to the web application by using @EnableWebMvc or <;mvc:annotation-driven>; namespace. HTTPMessageConverter is responsible to convert the request body to a specific class and back to the response body again, depending on a predefined mime type.</p>
<p><b>Create the following @Controller class:</b></p>
<pre class="highlight">/**
 * 
 */
package com.doj.restapi.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.doj.restapi.bean.Account;
import com.doj.restapi.service.IAccountService;

/**
 * @author Dinesh.Rajput
 *
 */
@Controller
public class AccountController {
 
 @Autowired
 IAccountService accountService;
 
 @RequestMapping(value = "/", method = RequestMethod.GET)
 public @ResponseBody String home (){
 return "Spring REST Dinesh on Java!!!";
 }
 
 @RequestMapping(value = "/accounts", method = RequestMethod.GET)
 public @ResponseBody List<;Account>; all (){
 return accountService.list();
 }
 
 @RequestMapping(value = "/account", method = RequestMethod.POST)
 public @ResponseBody Account create (@RequestBody Account account){
 return accountService.create(account);
 }
 
 @RequestMapping(value = "/account/{accountId}", method = RequestMethod.GET)
 public @ResponseBody Account get (@PathVariable Long accountId){
 return accountService.get(accountId);
 }
 
 @RequestMapping(value = "/account", method = RequestMethod.PUT)
 public @ResponseBody Account update (@RequestBody Account account){
 return accountService.update(account);
 }
 
 @RequestMapping(value = "/account/{accountId}", method = RequestMethod.DELETE)
 public @ResponseBody void delete (@PathVariable Long accountId){
 accountService.delete(accountId);
 }
}
</pre>
<p>
Notice the @ResponseBody added to each of the @RequestMapping methods in the return value.</p>
<p><b>Using the @RestController Annotation</b><br />
@RestController annotation is introduced as of Spring 4.0, it is actually combination of @Controller and @ResponseBody. It is a convenience way to use this annotation instead of using @Controller and @ResponseBody annotations. By annotating the controller class with @RestController annotation, you no longer need to add @ResponseBody to all the request mapping methods. The @ResponseBody annotation is active by default.</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="http://50.87.249.200/~dineshon/wp-content/uploads/2017/04/Spring-REST-workflow-using-RestController.png" /></div>
<p><b><i>@ImageSource-https://www.genuitec.com</i></b><br />
<b><i><br />
</i></b> Let&#8217;s use @RestController in our example, all we need to do is modify the @Controller to @RestController and remove the @ResponseBody from each method. As following:</p>
<pre class="highlight">/**
 * 
 */
package com.doj.restapi.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.doj.restapi.bean.Account;
import com.doj.restapi.service.IAccountService;

/**
 * @author Dinesh.Rajput
 *
 */
@RestController
public class AccountController {
 
 @Autowired
 IAccountService accountService;
 
 @GetMapping("/")
 public String home (){
 return "Spring REST Dinesh on Java!!!";
 }
 
 @GetMapping("/accounts")
 public List<;Account>; all (){
 return accountService.list();
 }
 
 @PostMapping("/account")
 public Account create (@RequestBody Account account){
 return accountService.create(account);
 }
 
 @GetMapping("/account/{accountId}")
 public Account get (@PathVariable Long accountId){
 return accountService.get(accountId);
 }
 
 @PutMapping("/account")
 public Account update (@RequestBody Account account){
 return accountService.update(account);
 }
 
 @DeleteMapping("/account/{accountId}")
 public void delete (@PathVariable Long accountId){
 accountService.delete(accountId);
 }
}

</pre>
<p>
As per as above controller file with @RestController annotation, we can see, using this annotation is quite simple and is the preferred method for creating MVC RESTful web services if we are using Spring v4.0 and above.</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;"><b>Spring MVC Related Posts</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/2012/12/spring-web-mvc-framework-chapter-38.html">Spring MVC Web Tutorial</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/spring-mvc-interview-questions-and-answers.html">Spring MVC Interview Questions</a></b></li>
<li><b><a href="https://dineshonjava.com/2013/07/introduction-to-mvc.html">MVC Design Pattern</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/what-is-dispatcherservlet-in-spring-and-its-uses.html">Spring MVC DispatcherServlet </a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/difference-between-applicationcontext-webapplicationcontext-in-spring-mvc.html">Spring MVC WebApplicationContext and Root Application Context</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/stereotype-annotations-in-spring.html">Spring MVC @Controller Annotation</a></b></li>
<li><b><a href="https://dineshonjava.com/2016/03/requestmapping-annotation-in-spring-mvc.html">Spring MVC @RequestMapping Annotation</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/requestparam-annotation-in-spring-mvc-with-example.html">Spring MVC @RequestParam Annotation</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/difference-between-applicationcontext-webapplicationcontext-in-spring-mvc.html">Spring MVC ContextLoaderListener</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/requestparam-vs-pathvariable-annotations-in-spring-mvc.html">Spring MVC @RequestParam and @PathVariable annotations</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-30-mvc-hello-world-example.html">Spring MVC Hello World Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-exception-handling-example.html">Spring MVC Exception Handling Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-mvc-with-hibernate-crud-example.html">Spring MVC with Hibernate CRUD Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-3-mvc-tiles-plugin-with-example.html">Spring MVC Tiles Plugin with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-3-mvc-and-interceptor-with.html">Spring MVC Interceptor with example</a></b></li>
<li><b><a href="https://dineshonjava.com/2013/01/integration-spring-mvc3-and-mongodb.html">Spring MVC with MongoDB CRUD Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-3-mvc-internationalization.html">Spring MVC Internationalization &; Localization with Example</a></b></li>
</ol>
</div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/webmvcconfigureradapter-vs-webmvcconfigurationsupport-in-spring-mvc/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '35'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…