Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Oct 13, 2022, 8:36 AM EDT
@RestController is a convenience annotation that does nothing more than adds the @Controller and @ResponseBody annotations in single statement. A key difference between a traditional MVC @Controller and the RESTful web service @RestController is the way that the HTTP response body is created. Rather than relying on a view technology to perform server-side rendering of the data to HTML, rest controller simply populates and returns the domain object itself. The object data is be written directly to the HTTP response as JSON or XML and parsed by client to further process it either for modifying the existing view or for any other purpose.
Using @Controller in spring mvc application
@Controller @RequestMapping("employees") public class EmployeeController { @RequestMapping(value = "/{name}", method = RequestMethod.GET) public Employee getEmployeeByName(@PathVariable String name, Model model) { //pull data and set in model return employeeTemplate; } }Using @Controller with @ResponseBody in spring
@Controller @ResponseBody @RequestMapping("employees") public class EmployeeController { @RequestMapping(value = "/{name}", method = RequestMethod.GET, produces = "application/json") public Employee getEmployeeByName(@PathVariable String name) { //pull date return employee; } }Using @RestController in spring
@RestController @RequestMapping("employees") public class EmployeeController { @RequestMapping(value = "/{name}", method = RequestMethod.GET, produces = "application/json") public Employee getEmployeeByName(@PathVariable String name) { //pull date return employee; } }