{"id":17847,"date":"2025-06-18T15:07:58","date_gmt":"2025-06-18T07:07:58","guid":{"rendered":"https:\/\/92it.top\/?p=17847"},"modified":"2025-06-18T15:07:58","modified_gmt":"2025-06-18T07:07:58","slug":"spring-restcontroller-%e5%92%8c-controller-%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=17847","title":{"rendered":"Spring @RestController \u548c @Controller \u533a\u522b"},"content":{"rendered":"\n<p>\u5728 Spring \u6846\u67b6\u4e2d\uff0c<code>@RestController<\/code>\u00a0\u548c\u00a0<code>@Controller<\/code>\u00a0\u662f\u7528\u4e8e\u5b9a\u4e49\u63a7\u5236\u5668\u7ec4\u4ef6\u7684\u4e24\u4e2a\u6ce8\u89e3\uff0c\u5b83\u4eec\u7684\u6838\u5fc3\u533a\u522b\u5728\u4e8e\u00a0<strong>\u54cd\u5e94\u4f53\u7684\u5904\u7406\u65b9\u5f0f<\/strong>\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u5bf9\u6bd4\uff1a<\/p>\n\n\n\n<p><strong>\u4e00\u3001\u6838\u5fc3\u533a\u522b<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>\u6ce8\u89e3<\/th><th><code>@Controller<\/code><\/th><th><code>@RestController<\/code><\/th><\/tr><\/thead><tbody><tr><td><strong>\u54cd\u5e94\u4f53\u5904\u7406<\/strong><\/td><td>\u9700\u8981\u914d\u5408&nbsp;<code>@ResponseBody<\/code>&nbsp;\u8fd4\u56de JSON\/XML<\/td><td>\u76f4\u63a5\u8fd4\u56de JSON\/XML\uff08\u9ed8\u8ba4\uff09<\/td><\/tr><tr><td><strong>\u89c6\u56fe\u89e3\u6790<\/strong><\/td><td>\u652f\u6301\u89c6\u56fe\u89e3\u6790\uff08\u5982\u8fd4\u56de HTML \u9875\u9762\uff09<\/td><td>\u4e0d\u652f\u6301\u89c6\u56fe\u89e3\u6790\uff0c\u76f4\u63a5\u8fd4\u56de\u6570\u636e<\/td><\/tr><tr><td><strong>\u5178\u578b\u5e94\u7528\u573a\u666f<\/strong><\/td><td>MVC \u67b6\u6784\u4e2d\u7684\u9875\u9762\u63a7\u5236\u5668<\/td><td>RESTful API \u670d\u52a1<\/td><\/tr><tr><td><strong>\u51fa\u73b0\u7248\u672c<\/strong><\/td><td>Spring MVC<\/td><td>Spring 4.0 \u5f15\u5165<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>\u4e8c\u3001\u4ee3\u7801\u793a\u4f8b\u5bf9\u6bd4<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>1.\u00a0<code>@Controller<\/code>\u00a0+\u00a0<code>@ResponseBody<\/code><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Controller\npublic class UserController {\n\n    @GetMapping(\"\/users\/{id}\")\n    @ResponseBody  \/\/ \u5fc5\u987b\u6dfb\u52a0\u8be5\u6ce8\u89e3\u624d\u80fd\u8fd4\u56de JSON\n    public User getUser(@PathVariable Long id) {\n        return userService.findById(id);\n    }\n\n    @GetMapping(\"\/home\")\n    public String home(Model model) {\n        model.addAttribute(\"message\", \"Hello World\");\n        return \"home\";  \/\/ \u8fd4\u56de\u89c6\u56fe\u540d\u79f0\uff0c\u7531\u89c6\u56fe\u89e3\u6790\u5668\u5904\u7406\n    }\n}<\/pre>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p>2.\u00a0<code>@RestController<\/code><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@RestController  \/\/ \u7b49\u4ef7\u4e8e @Controller + @ResponseBody\npublic class UserRestController {\n\n    @GetMapping(\"\/api\/users\/{id}\")\n    public User getUser(@PathVariable Long id) {\n        return userService.findById(id);  \/\/ \u76f4\u63a5\u8fd4\u56de\u5bf9\u8c61\uff0c\u81ea\u52a8\u5e8f\u5217\u5316\u4e3a JSON\n    }\n\n    @GetMapping(\"\/api\/message\")\n    public String getMessage() {\n        return \"This is a REST API\";  \/\/ \u8fd4\u56de\u5b57\u7b26\u4e32\uff0c\u800c\u975e\u89c6\u56fe\u540d\u79f0\n    }\n}<\/pre>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u4e09\u3001\u6280\u672f\u7ec6\u8282\u5bf9\u6bd4<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>\u7279\u6027<\/th><th><code>@Controller<\/code><\/th><th><code>@RestController<\/code><\/th><\/tr><\/thead><tbody><tr><td><strong>\u6ce8\u89e3\u672c\u8d28<\/strong><\/td><td>\u662f&nbsp;<code>@Component<\/code>&nbsp;\u7684\u6d3e\u751f\u6ce8\u89e3<\/td><td>\u662f&nbsp;<code>@Controller<\/code>&nbsp;\u548c&nbsp;<code>@ResponseBody<\/code>&nbsp;\u7684\u7ec4\u5408\u6ce8\u89e3<\/td><\/tr><tr><td><strong>\u8fd4\u56de\u503c\u5904\u7406<\/strong><\/td><td>&#8211; \u8fd4\u56de&nbsp;<code>String<\/code>\uff1a\u89c6\u56fe\u540d\u79f0<br>&#8211; \u8fd4\u56de\u5bf9\u8c61\uff1a\u9700&nbsp;<code>@ResponseBody<\/code><\/td><td>&#8211; \u8fd4\u56de&nbsp;<code>String<\/code>\uff1a\u5b57\u7b26\u4e32\u5185\u5bb9<br>&#8211; \u8fd4\u56de\u5bf9\u8c61\uff1a\u81ea\u52a8 JSON \u5e8f\u5217\u5316<\/td><\/tr><tr><td><strong>\u89c6\u56fe\u89e3\u6790<\/strong><\/td><td>\u4f9d\u8d56&nbsp;<code>ViewResolver<\/code>&nbsp;\u89e3\u6790\u89c6\u56fe<\/td><td>\u5ffd\u7565\u89c6\u56fe\u89e3\u6790\uff0c\u76f4\u63a5\u8fd4\u56de\u6570\u636e<\/td><\/tr><tr><td><strong>\u54cd\u5e94\u683c\u5f0f<\/strong><\/td><td>\u9700\u624b\u52a8\u914d\u7f6e&nbsp;<code>produces<\/code>&nbsp;\u5c5e\u6027\u6216\u6d88\u606f\u8f6c\u6362\u5668<\/td><td>\u9ed8\u8ba4 JSON\uff0c\u53ef\u901a\u8fc7&nbsp;<code>produces<\/code>&nbsp;\u8986\u76d6<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>\u56db\u3001\u5e38\u89c1\u5e94\u7528\u573a\u666f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>1.&nbsp;<code>@Controller<\/code>&nbsp;\u7684\u9002\u7528\u573a\u666f<\/p>\n\n\n\n<ul>\n<li>\u4f20\u7edf MVC \u5e94\u7528\uff0c\u9700\u8981\u8fd4\u56de HTML \u9875\u9762\u3002<\/li>\n\n\n\n<li>\u670d\u52a1\u7aef\u6e32\u67d3\uff08\u5982 Thymeleaf\u3001JSP\uff09\u3002<\/li>\n\n\n\n<li>\u9700\u8981\u590d\u6742\u89c6\u56fe\u903b\u8f91\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n\n\n\n<p>2.&nbsp;<code>@RestController<\/code>&nbsp;\u7684\u9002\u7528\u573a\u666f<\/p>\n\n\n\n<ul>\n<li>RESTful API \u5f00\u53d1\u3002<\/li>\n\n\n\n<li>\u524d\u540e\u7aef\u5206\u79bb\u67b6\u6784\u3002<\/li>\n\n\n\n<li>\u5fae\u670d\u52a1\u95f4\u901a\u4fe1\uff08\u5982\u8fd4\u56de JSON\/XML \u6570\u636e\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u3000\u4e94\u3001\u5173\u952e\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u907f\u514d\u6df7\u7528<\/strong><\/p>\n\n\n\n<ul>\n<li>\u5728\u00a0<code>@RestController<\/code>\u00a0\u4e2d\u4f7f\u7528\u00a0<code>@ResponseBody<\/code>\u00a0\u662f\u591a\u4f59\u7684\u3002<\/li>\n\n\n\n<li>\u5728\u00a0<code>@Controller<\/code>\u00a0\u4e2d\u4e0d\u4f7f\u7528\u00a0<code>@ResponseBody<\/code>\u00a0\u4f1a\u5bfc\u81f4\u8fd4\u56de\u89c6\u56fe\u89e3\u6790\u9519\u8bef\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u54cd\u5e94\u683c\u5f0f\u5b9a\u5236<\/strong><\/p>\n\n\n\n<ul>\n<li>\u901a\u8fc7\u00a0<code>produces<\/code>\u00a0\u5c5e\u6027\u6307\u5b9a\u54cd\u5e94\u7c7b\u578b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@GetMapping(path = \"\/data\", produces = MediaType.APPLICATION_XML_VALUE)\npublic User getData() { ... }<\/pre>\n\n\n\n<p><strong>\u517c\u5bb9\u6027<\/strong><\/p>\n\n\n\n<ul>\n<li><code>@RestController<\/code>\u00a0\u662f Spring 4.0 \u5f15\u5165\u7684\uff0c\u82e5\u4f7f\u7528\u65e7\u7248\u672c Spring\uff0c\u9700\u7528\u00a0<code>@Controller + @ResponseBody<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u3000<\/p>\n\n\n\n<p><strong>\u516d\u3001\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<ul>\n<li><strong>\u9009\u00a0<code>@RestController<\/code><\/strong>\uff1a\u5982\u679c\u4f60\u7684\u63a7\u5236\u5668\u662f\u7528\u4e8e\u63d0\u4f9b REST API\uff0c\u8fd4\u56de JSON\/XML \u7b49\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>\u9009\u00a0<code>@Controller<\/code><\/strong>\uff1a\u5982\u679c\u9700\u8981\u8fd4\u56de\u89c6\u56fe\uff08\u5982 HTML \u9875\u9762\uff09\uff0c\u6216\u9700\u8981\u6df7\u5408\u4f7f\u7528\u89c6\u56fe\u548c JSON \u54cd\u5e94<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Spring \u6846\u67b6\u4e2d\uff0c@RestController\u00a0\u548c\u00a0@Controller\u00a0\u662f\u7528\u4e8e\u5b9a\u4e49\u63a7\u5236\u5668\u7ec4\u4ef6\u7684\u4e24 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17847"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17847"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17847\/revisions"}],"predecessor-version":[{"id":17848,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17847\/revisions\/17848"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}