{"id":315,"date":"2020-05-11T11:00:00","date_gmt":"2020-05-11T18:00:00","guid":{"rendered":"https:\/\/www.tizianasellitto.it\/blog\/?p=315"},"modified":"2020-05-11T09:47:55","modified_gmt":"2020-05-11T16:47:55","slug":"spring-restcontroller-vs-controller","status":"publish","type":"post","link":"https:\/\/www.tizianasellitto.it\/blog\/?p=315","title":{"rendered":"Spring @RestController vs @Controller"},"content":{"rendered":"\n<p>To understand the difference between <strong>@RestController<\/strong> and <strong>@Controller<\/strong> we can think about the main difference between a <strong>REST API<\/strong> and a <strong>Web application.<\/strong> The response of a REST API is generally JSON or XML; in a web application, instead, the response is usually a view (some html + css) intended for human viewers.<\/p>\n\n\n\n<p class=\"has-text-align-justify\">This is also the main difference between @Controller\u00a0and\u00a0@RestController\u00a0annotation. Basically in a spring mvc <strong>@Controller<\/strong> the handler method returns the response \u201cview name\u201d which is resolved to a view technology file (e.g. JSP) and the parsed view content is sent back to browser client. It just create a map of the model object and find a view. If we want instead to bundle the return value yo a web response body we can use the <strong>@ResponseBody<\/strong> annotation and no view resolver is needed.  <\/p>\n\n\n\n<p>For example assuming that we have a view and a greeting Api in a MVCController that take a param as input and return a view we can use @Controller annotation<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Path of view template\n\/src\/main\/resources\/templates\/greeting.html\n\n@Controller\npublic class MVCController {\n\n    @RequestMapping(\"\/greeting\")\n    public String greeting(@RequestParam(name = \"name\", required = \n              false, defaultValue = \"World\") String name, Model model) {\n        model.addAttribute(\"name\", name);\n        \/\/ We can return a view name that is present in \n        return \"greeting\";\n    }\n}<\/pre>\n\n\n\n<p class=\"has-text-align-justify\">The <strong>@RestController<\/strong>\u00a0simply returns the object and object data is directly written into HTTP response as JSON or XML. One notice is that in Spring MVC the\u00a0@RestController\u00a0annotation is a combination of\u00a0@Controller\u00a0and\u00a0@ResponseBody\u00a0annotation.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">@Controller\n@ResponseBody\npublic class MVCController {\n  \/\/ your logic\n}\n\n@RestController\npublic class RestFulController { \n  \/\/ your logic\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To understand the difference between @RestController and @Controller we can think about the main difference between a REST API and a Web application. The response of a REST API is generally JSON or XML; in a web application, instead, the response is usually a view (some html + css) intended for human viewers. This is [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19,14],"tags":[32],"_links":{"self":[{"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=\/wp\/v2\/posts\/315"}],"collection":[{"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=315"}],"version-history":[{"count":6,"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=\/wp\/v2\/posts\/315\/revisions"}],"predecessor-version":[{"id":321,"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=\/wp\/v2\/posts\/315\/revisions\/321"}],"wp:attachment":[{"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tizianasellitto.it\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}