What is the difference between controller and restcontroller?
Answers
Answered by
0
Difference between @Controller vs @RestController in Spring MVC
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.
Traditional Spring MVC Workflow
The following image describe a traditional Spring MVC Request workflow:

@ImageSource-https://www.genuitec.com
Step 1: The browser or client sends a request to the web application.Step 2: The request lands to the dispachter servlet and consult with handler mapping.Step 3: The Handler mappings defined in the application context file, this has information about which is controller need to be invoked.Step 4: The controller will be invoked and it can request the model for some information using DAOs and Services.Step 5: After Requests are processed by the Controller and the response is returned to the DispatcherServlet with logical view nameStep 6: Then return to the view resolver prepared the view based on your configuration decide the which configuration (JSP, Velocity, PDF etc.) to be invoked.
For Example:
@Controller
public class AccountController {
@RequestMapping(value="/account", method=RequestMethod.GET)
public ModelAndView get(Long accountId) {
//Process model object
return new ModelAndView("account-detail", model) ;
}
}
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.
Traditional Spring MVC Workflow
The following image describe a traditional Spring MVC Request workflow:

@ImageSource-https://www.genuitec.com
Step 1: The browser or client sends a request to the web application.Step 2: The request lands to the dispachter servlet and consult with handler mapping.Step 3: The Handler mappings defined in the application context file, this has information about which is controller need to be invoked.Step 4: The controller will be invoked and it can request the model for some information using DAOs and Services.Step 5: After Requests are processed by the Controller and the response is returned to the DispatcherServlet with logical view nameStep 6: Then return to the view resolver prepared the view based on your configuration decide the which configuration (JSP, Velocity, PDF etc.) to be invoked.
For Example:
@Controller
public class AccountController {
@RequestMapping(value="/account", method=RequestMethod.GET)
public ModelAndView get(Long accountId) {
//Process model object
return new ModelAndView("account-detail", model) ;
}
}
Similar questions