{"id":5163,"date":"2019-02-06T18:40:22","date_gmt":"2019-02-06T13:10:22","guid":{"rendered":"https:\/\/www.innovationm.com\/blog\/?p=5163"},"modified":"2023-01-20T18:55:32","modified_gmt":"2023-01-20T13:25:32","slug":"difference-between-the-controller-and-restcontroller","status":"publish","type":"post","link":"https:\/\/www.innovationm.com\/blog\/difference-between-the-controller-and-restcontroller\/","title":{"rendered":"Difference Between the @Controller and @RestController"},"content":{"rendered":"<p>Spring Framework introduced @Controller and @RestController annotations to create a controller. Basically, the controller is used to handling the client requests for a particular resource.<\/p>\n<p><strong>For example<\/strong><\/p>\n<p><a href=\"http:\/\/mydomain.com\/home\">mydomain.com\/home<\/a><\/p>\n<p>This request goes to dispatcher servlet then it will be forwarded to controller corresponding methods with that URL mapping<\/p>\n<p>If you create your controller using @Controller annotation then your controller class methods must be resolve mapping URL with a view if they are not returning response.<\/p>\n<pre class=\"lang:java decode:true\">@Controller\r\npublic class MyController{\r\n@RequestMapping(\"\/home\")\r\npublic String home(){\r\n\r\nreturn \"home.jsp\";\r\n}\r\n\r\n@RequestMapping(\"\/welcome\")\r\npublic ModelAndView welcome(){\r\n\r\nModelAndView mv=new ModelAndView(\"home.jsp\");\r\nmv.addObject(\"msg\",\"welcome\");\r\nreturn mv;\r\n\r\n}\r\n\r\n}\r\n<\/pre>\n<p>Here home() method will resolve the respective view for\u00a0 <a href=\"http:\/\/mydomain.com\/home\">http:\/\/mydomain.com\/home\u00a0<\/a>URL and welcome() method return model data which will be available on respective view home.jsp<\/p>\n<p>@Controller is used to add a model object(which holds data) in your view but it can also be used as return only response( return data in JSON format).<br \/>\nfor returning only response from controller class you have to write one more annotation @ResponseBody which basically convert your object to response.<br \/>\nyou have to write @ResponseBody on your method to product only response.<\/p>\n<pre class=\"lang:java decode:true \">@Controller\r\npublic class MyController{\r\n@RequestMapping(value=\"\/user\")\r\n@ResponseBody\r\npublic User getUser()\r\n\r\n\/\/reterival operations\r\nreturn user;\r\n}\r\n}\r\n<\/pre>\n<p>But if you want to create Restful API then you have to annotate each method with @ResponseBody<br \/>\nThat&#8217;s why spring 4.o introduced another annotation @RestController to create a controller.<br \/>\nif you create your controller using @RestController annotation then all methods of this controller class by default return response and you don&#8217;t need any more @ResponseBody annotation to return response from methods.<\/p>\n<pre class=\"lang:default decode:true \">@RestController\r\npublic class MyController{\r\n@RequestMapping(value=\"\/user\")\r\npublic User getUser()\r\n\r\n\/\/reterival operations\r\nreturn user;\r\n}\r\n}\r\n\r\n\r\n\r\n<\/pre>\n<p>@Controller is a specialization of the\u00a0<em>@Component<\/em>\u00a0class and @RestController is\u00a0a specialization of the\u00a0<em>@Controller<\/em><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Framework introduced @Controller and @RestController annotations to create a controller. Basically, the controller is used to handling the client requests for a particular resource. For example mydomain.com\/home This request goes to dispatcher servlet then it will be forwarded to controller corresponding methods with that URL mapping If you create your controller using @Controller annotation [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5165,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[256],"tags":[324,326,325,279],"class_list":["post-5163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-application","tag-controller","tag-response-body","tag-restcontroller","tag-spring-boot"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Difference Between the @Controller and @RestController - InnovationM - Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.innovationm.com\/blog\/difference-between-the-controller-and-restcontroller\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Difference Between the @Controller and @RestController - InnovationM - Blog\" \/>\n<meta property=\"og:description\" content=\"Spring Framework introduced @Controller and @RestController annotations to create a controller. Basically, the controller is used to handling the client requests for a particular resource. For example mydomain.com\/home This request goes to dispatcher servlet then it will be forwarded to controller corresponding methods with that URL mapping If you create your controller using @Controller annotation [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.innovationm.com\/blog\/difference-between-the-controller-and-restcontroller\/\" \/>\n<meta property=\"og:site_name\" content=\"InnovationM - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-02-06T13:10:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-20T13:25:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/02\/blog-new-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1151\" \/>\n\t<meta property=\"og:image:height\" content=\"640\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"InnovationM Admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InnovationM Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/difference-between-the-controller-and-restcontroller\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/difference-between-the-controller-and-restcontroller\\\/\"},\"author\":{\"name\":\"InnovationM Admin\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"headline\":\"Difference Between the @Controller and @RestController\",\"datePublished\":\"2019-02-06T13:10:22+00:00\",\"dateModified\":\"2023-01-20T13:25:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/difference-between-the-controller-and-restcontroller\\\/\"},\"wordCount\":254,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/difference-between-the-controller-and-restcontroller\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/02\\\/blog-new-1.png\",\"keywords\":[\"controller\",\"response body\",\"restcontroller\",\"Spring Boot\"],\"articleSection\":[\"Java Application\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/difference-between-the-controller-and-restcontroller\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/difference-between-the-controller-and-restcontroller\\\/\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/difference-between-the-controller-and-restcontroller\\\/\",\"name\":\"Difference Between the @Controller and @RestController - InnovationM - Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/difference-between-the-controller-and-restcontroller\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/difference-between-the-controller-and-restcontroller\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/02\\\/blog-new-1.png\",\"datePublished\":\"2019-02-06T13:10:22+00:00\",\"dateModified\":\"2023-01-20T13:25:32+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/difference-between-the-controller-and-restcontroller\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/difference-between-the-controller-and-restcontroller\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/difference-between-the-controller-and-restcontroller\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/02\\\/blog-new-1.png\",\"contentUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/02\\\/blog-new-1.png\",\"width\":1151,\"height\":640,\"caption\":\"controller, restcontroller, spring boot\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/difference-between-the-controller-and-restcontroller\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Difference Between the @Controller and @RestController\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\",\"name\":\"InnovationM - Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\",\"name\":\"InnovationM Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"caption\":\"InnovationM Admin\"},\"sameAs\":[\"http:\\\/\\\/www.innovationm.com\\\/\"],\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/author\\\/innovationmadmin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Difference Between the @Controller and @RestController - InnovationM - Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.innovationm.com\/blog\/difference-between-the-controller-and-restcontroller\/","og_locale":"en_US","og_type":"article","og_title":"Difference Between the @Controller and @RestController - InnovationM - Blog","og_description":"Spring Framework introduced @Controller and @RestController annotations to create a controller. Basically, the controller is used to handling the client requests for a particular resource. For example mydomain.com\/home This request goes to dispatcher servlet then it will be forwarded to controller corresponding methods with that URL mapping If you create your controller using @Controller annotation [&hellip;]","og_url":"https:\/\/www.innovationm.com\/blog\/difference-between-the-controller-and-restcontroller\/","og_site_name":"InnovationM - Blog","article_published_time":"2019-02-06T13:10:22+00:00","article_modified_time":"2023-01-20T13:25:32+00:00","og_image":[{"width":1151,"height":640,"url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/02\/blog-new-1.png","type":"image\/png"}],"author":"InnovationM Admin","twitter_misc":{"Written by":"InnovationM Admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.innovationm.com\/blog\/difference-between-the-controller-and-restcontroller\/#article","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/difference-between-the-controller-and-restcontroller\/"},"author":{"name":"InnovationM Admin","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"headline":"Difference Between the @Controller and @RestController","datePublished":"2019-02-06T13:10:22+00:00","dateModified":"2023-01-20T13:25:32+00:00","mainEntityOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/difference-between-the-controller-and-restcontroller\/"},"wordCount":254,"commentCount":0,"image":{"@id":"https:\/\/www.innovationm.com\/blog\/difference-between-the-controller-and-restcontroller\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/02\/blog-new-1.png","keywords":["controller","response body","restcontroller","Spring Boot"],"articleSection":["Java Application"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.innovationm.com\/blog\/difference-between-the-controller-and-restcontroller\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.innovationm.com\/blog\/difference-between-the-controller-and-restcontroller\/","url":"https:\/\/www.innovationm.com\/blog\/difference-between-the-controller-and-restcontroller\/","name":"Difference Between the @Controller and @RestController - InnovationM - Blog","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/difference-between-the-controller-and-restcontroller\/#primaryimage"},"image":{"@id":"https:\/\/www.innovationm.com\/blog\/difference-between-the-controller-and-restcontroller\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/02\/blog-new-1.png","datePublished":"2019-02-06T13:10:22+00:00","dateModified":"2023-01-20T13:25:32+00:00","author":{"@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"breadcrumb":{"@id":"https:\/\/www.innovationm.com\/blog\/difference-between-the-controller-and-restcontroller\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.innovationm.com\/blog\/difference-between-the-controller-and-restcontroller\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.innovationm.com\/blog\/difference-between-the-controller-and-restcontroller\/#primaryimage","url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/02\/blog-new-1.png","contentUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2019\/02\/blog-new-1.png","width":1151,"height":640,"caption":"controller, restcontroller, spring boot"},{"@type":"BreadcrumbList","@id":"https:\/\/www.innovationm.com\/blog\/difference-between-the-controller-and-restcontroller\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.innovationm.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Difference Between the @Controller and @RestController"}]},{"@type":"WebSite","@id":"https:\/\/www.innovationm.com\/blog\/#website","url":"https:\/\/www.innovationm.com\/blog\/","name":"InnovationM - Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.innovationm.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed","name":"InnovationM Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","caption":"InnovationM Admin"},"sameAs":["http:\/\/www.innovationm.com\/"],"url":"https:\/\/www.innovationm.com\/blog\/author\/innovationmadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/5163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/comments?post=5163"}],"version-history":[{"count":0,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/5163\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media\/5165"}],"wp:attachment":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media?parent=5163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/categories?post=5163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/tags?post=5163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}