{"id":8329,"date":"2023-11-28T20:25:06","date_gmt":"2023-11-28T12:25:06","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=8329"},"modified":"2023-12-06T10:20:32","modified_gmt":"2023-12-06T02:20:32","slug":"springboot-zhongrestcontroller-hecontroller-de-qu","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springboot-zhongrestcontroller-hecontroller-de-qu\/","title":{"rendered":"SpringBoot \u4e2d@RestController\t\u548c@Controller\u7684\u533a\u522b\uff1f"},"content":{"rendered":"<p><code>@RestController<\/code> \u548c <code>@Controller<\/code> \u662f Spring Framework \u4e2d\u7528\u4e8e\u5b9a\u4e49\u63a7\u5236\u5668\u7684\u6ce8\u89e3\uff0c\u5b83\u4eec\u4e4b\u95f4\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u5b83\u4eec\u7684\u9ed8\u8ba4\u884c\u4e3a\u548c\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<ol>\n<li>\n<strong><code>@Controller<\/code>\uff1a<\/strong><\/p>\n<ul>\n<li><code>@Controller<\/code> \u7528\u4e8e\u5b9a\u4e49\u63a7\u5236\u5668\u7c7b\uff0c\u901a\u5e38\u914d\u5408 <code>@RequestMapping<\/code> \u6ce8\u89e3\u4e00\u8d77\u4f7f\u7528\uff0c\u7528\u4e8e\u5904\u7406 HTTP \u8bf7\u6c42\u548c\u6784\u5efa\u89c6\u56fe\u3002<\/li>\n<li>\u63a7\u5236\u5668\u65b9\u6cd5\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u89c6\u56fe\u540d\uff0cSpring MVC \u4f1a\u6839\u636e\u89c6\u56fe\u540d\u89e3\u6790\u5e76\u8fd4\u56de\u76f8\u5e94\u7684\u89c6\u56fe\u9875\u9762\u3002<\/li>\n<li>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u63a7\u5236\u5668\u65b9\u6cd5\u4f7f\u7528 <code>@ResponseBody<\/code> \u6765\u5c06\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u76f4\u63a5\u5199\u5165 HTTP \u54cd\u5e94\u4f53\u4e2d\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Controller\npublic class MyController {\n\n    @RequestMapping(&quot;\/hello&quot;)\n    public String hello() {\n        return &quot;hello&quot;; \/\/ \u8fd4\u56de\u89c6\u56fe\u540d\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong><code>@RestController<\/code>\uff1a<\/strong><\/p>\n<ul>\n<li><code>@RestController<\/code> \u662f <code>@Controller<\/code> \u7684\u7279\u5316\u7248\u672c\uff0c\u7528\u4e8e\u6807\u8bb0 RESTful \u98ce\u683c\u7684\u63a7\u5236\u5668\u3002<\/li>\n<li>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>@RestController<\/code> \u7684\u6bcf\u4e2a\u65b9\u6cd5\u90fd\u4f7f\u7528 <code>@ResponseBody<\/code>\uff0c\u8868\u793a\u8be5\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u76f4\u63a5\u4f5c\u4e3a HTTP \u54cd\u5e94\u7684\u5185\u5bb9\uff0c\u800c\u4e0d\u662f\u89c6\u56fe\u3002<\/li>\n<li>\u5e38\u7528\u4e8e\u6784\u5efa RESTful API\uff0c\u901a\u5e38\u8fd4\u56de JSON \u6216 XML \u683c\u5f0f\u7684\u6570\u636e\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@RestController\npublic class MyRestController {\n\n    @RequestMapping(&quot;\/api\/hello&quot;)\n    public String hello() {\n        return &quot;Hello, REST!&quot;;\n    }\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u603b\u7684\u6765\u8bf4\uff0c<code>@Controller<\/code> \u7528\u4e8e\u4f20\u7edf\u7684 Web \u5e94\u7528\u7a0b\u5e8f\uff0c\u8fd4\u56de\u7684\u662f\u89c6\u56fe\u9875\u9762\uff1b\u800c <code>@RestController<\/code> \u7528\u4e8e\u6784\u5efa RESTful \u98ce\u683c\u7684\u670d\u52a1\uff0c\u8fd4\u56de\u7684\u662f\u6570\u636e\uff08JSON \u6216 XML\uff09\u3002\u5728\u4f7f\u7528\u65f6\uff0c\u6839\u636e\u5e94\u7528\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6ce8\u89e3\u3002\u5982\u679c\u9700\u8981\u8fd4\u56de\u89c6\u56fe\u9875\u9762\uff0c\u4f7f\u7528 <code>@Controller<\/code>\uff1b\u5982\u679c\u9700\u8981\u8fd4\u56de\u6570\u636e\uff0c\u4f7f\u7528 <code>@RestController<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>@RestController \u548c @Controller \u662f Spring Framework \u4e2d\u7528\u4e8e\u5b9a\u4e49\u63a7 [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"class_list":["post-8329","post","type-post","status-publish","format-standard","hentry","category-springboot"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=8329"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8329\/revisions"}],"predecessor-version":[{"id":16597,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8329\/revisions\/16597"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=8329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=8329"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=8329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}