{"id":210246,"date":"2021-01-26T10:00:18","date_gmt":"2021-01-26T02:00:18","guid":{"rendered":"https:\/\/gulass.cn\/?p=210246"},"modified":"2021-01-24T15:03:39","modified_gmt":"2021-01-24T07:03:39","slug":"controller-rest-difference","status":"publish","type":"post","link":"https:\/\/gulass.cn\/controller-rest-difference.html","title":{"rendered":"Controller\u548cRestController\u7684\u533a\u522b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n \u5728springboot\u4e2d\uff0cController, RestController\u662f\u4f7f\u7528\u63a7\u5236\u5668\u6700\u5e38\u7528\u7684\u4e24\u4e2a\u6ce8\u89e3\uff0c\u4f46\u662f\u4e24\u8005\u4e4b\u5bb6\u7684\u5dee\u5f02\u4f60\u77e5\u9053\u5417\uff1f\u672c\u6587\u5c31\u662f\u8981\u8bb2\u8ff0\u4e24\u8005\u4e4b\u95f4\u7684\u533a\u522b\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
1. Controller, RestController\u7684\u5171\u540c\u70b9<\/strong><\/div>\n

\u90fd\u662f\u7528\u6765\u8868\u793aSpring\u67d0\u4e2a\u7c7b\u7684\u662f\u5426\u53ef\u4ee5\u63a5\u6536HTTP\u8bf7\u6c42\u3002<\/p>\n

2. Controller, RestController\u7684\u4e0d\u540c\u70b9<\/strong><\/div>\n

@Controller\uff1a\u6807\u8bc6\u4e00\u4e2aSpring\u7c7b\u662fSpring MVC controller\u5904\u7406\u5668,@RestController\uff1a@RestController\u662f@Controller\u548c@ResponseBody\u7684\u7ed3\u5408\u4f53\uff0c\u4e24\u4e2a\u6807\u6ce8\u5408\u5e76\u8d77\u6765\u7684\u4f5c\u7528\u3002@Controller\u7c7b\u4e2d\u7684\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u8fd4\u56deString\u8df3\u8f6c\u5230jsp\u3001ftl\u3001html\u7b49\u6a21\u7248\u9875\u9762\u3002\u5728\u65b9\u6cd5\u4e0a\u52a0@ResponseBody\u6ce8\u89e3\uff0c\u4e5f\u53ef\u4ee5\u8fd4\u56de\u5b9e\u4f53\u5bf9\u8c61\u3002@RestController\u7c7b\u4e2d\u7684\u6240\u6709\u65b9\u6cd5\u53ea\u80fd\u8fd4\u56deString\u3001Object\u3001Json\u7b49\u5b9e\u4f53\u5bf9\u8c61\uff0c\u4e0d\u80fd\u8df3\u8f6c\u5230\u6a21\u7248\u9875\u9762\u3002<\/p>\n

@RestController\u4e2d\u7684\u65b9\u6cd5\u5982\u679c\u60f3\u8df3\u8f6c\u9875\u9762\uff0c\u5219\u7528ModelAndView\u8fdb\u884c\u5c01\u88c5\uff0c\u5982\u4e0b\uff1a<\/p>\n

\r\n@RestController\r\npublic class UserController {\r\n\r\n    @RequestMapping(value = \"\/index\",method = RequestMethod.GET)\r\n    public String toIndex(){\r\n        ModelAndView mv = new ModelAndView(\"index\");\r\n      \treturn mv;    \r\n    }\r\n}\r\n \r\n<\/pre>\n

\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n

\r\n@Controller  \r\n@ResponseBody  \r\npublic class MyController { }  \r\n\r\n@RestController  \r\npublic class MyRestController { }  \r\n<\/pre>\n

@Controller\u6ce8\u89e3\u6e90\u7801:<\/p>\n

\r\npackage org.springframework.stereotype;\r\n\r\nimport java.lang.annotation.Documented;\r\nimport java.lang.annotation.ElementType;\r\nimport java.lang.annotation.Retention;\r\nimport java.lang.annotation.RetentionPolicy;\r\nimport java.lang.annotation.Target;\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Target({ElementType.TYPE})\r\n@Retention(RetentionPolicy.RUNTIME)\r\n@Documented\r\n@Component\r\npublic @interface Controller {\r\n    String value() default \"\";\r\n}\r\n<\/pre>\n

@RestController\u6ce8\u89e3\u6e90\u7801:<\/p>\n

\r\npackage org.springframework.web.bind.annotation;\r\n\r\nimport java.lang.annotation.Documented;\r\nimport java.lang.annotation.ElementType;\r\nimport java.lang.annotation.Retention;\r\nimport java.lang.annotation.RetentionPolicy;\r\nimport java.lang.annotation.Target;\r\nimport org.springframework.stereotype.Controller;\r\nimport org.springframework.web.bind.annotation.ResponseBody;\r\n\r\n@Target({ElementType.TYPE})\r\n@Retention(RetentionPolicy.RUNTIME)\r\n@Documented\r\n@Controller\r\n@ResponseBody\r\npublic @interface RestController {\r\n    String value() default \"\";\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u90fd\u662f\u7528\u6765\u8868\u793aSpring\u67d0\u4e2a\u7c7b\u7684\u662f\u5426\u53ef\u4ee5\u63a5\u6536HTTP\u8bf7\u6c42\u3002 @Controller\uff1a\u6807\u8bc6\u4e00\u4e2aSpring\u7c7b\u662fS […]<\/p>\n","protected":false},"author":1903,"featured_media":210319,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-210246","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/210246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=210246"}],"version-history":[{"count":6,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/210246\/revisions"}],"predecessor-version":[{"id":210377,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/210246\/revisions\/210377"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/210319"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=210246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=210246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=210246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}