{"id":1636,"date":"2025-03-13T14:01:25","date_gmt":"2025-03-13T06:01:25","guid":{"rendered":"https:\/\/www.tqazy.com\/?p=1636"},"modified":"2025-03-13T16:04:38","modified_gmt":"2025-03-13T08:04:38","slug":"restful-api%e9%a3%8e%e6%a0%bc%e7%9a%84controller%e4%b8%ad%e7%9a%84%e5%b8%b8%e7%94%a8%e6%b3%a8%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.tqazy.com\/?p=1636","title":{"rendered":"RESTful API\u98ce\u683c\u7684Controller\u4e2d\u7684\u5e38\u7528\u6ce8\u89e3"},"content":{"rendered":"<p>\u5728 RESTful API \u98ce\u683c\u7684\u63a5\u53e3\u5f00\u53d1\u4e2d\uff0cSpring \u6846\u67b6\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5e38\u7528\u7684\u6ce8\u89e3\uff0c\u8fd9\u4e9b\u6ce8\u89e3\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u65b9\u4fbf\u5730\u5b9a\u4e49\u548c\u5904\u7406 HTTP \u8bf7\u6c42\u3002<\/p>\n<p>\u4f7f\u7528\u4e4b\u524d\u9700\u8981\u6dfb\u52a0\u5982\u4e0b\u4f9d\u8d56\uff1a<\/p>\n<pre><code class=\"language-xml\">        &lt;dependency&gt;\n            &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n            &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\n        &lt;\/dependency&gt;<\/code><\/pre>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u6ce8\u89e3\u7684\u4ecb\u7ecd\u3001\u4f7f\u7528\u573a\u666f\u548c\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h1>\u4e00\u3001\u63a7\u5236\u7c7b\u76f8\u5173\u6ce8\u89e3<\/h1>\n<h2>1.1 <code>@RestController<\/code><\/h2>\n<p><code>@RestController<\/code> \u662f Spring \u6846\u67b6\u4e2d\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6ce8\u89e3\uff0c\u4e3b\u8981\u7528\u4e8e\u521b\u5efa RESTful \u98ce\u683c\u7684\u63a7\u5236\u5668\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h3>1.1.1 \u4ecb\u7ecd<\/h3>\n<p><code>@RestController<\/code> \u662f\u4e00\u4e2a\u7ec4\u5408\u6ce8\u89e3\uff0c\u5b83\u7ed3\u5408\u4e86 <code>@Controller<\/code> \u548c <code>@ResponseBody<\/code> \u7684\u529f\u80fd\u3002<br \/>\n<code>@Controller<\/code> \u7528\u4e8e\u6807\u8bb0\u4e00\u4e2a\u7c7b\u4e3a Spring MVC \u7684\u63a7\u5236\u5668\u3002<br \/>\n<code>@ResponseBody<\/code> \u7528\u4e8e\u5c06\u63a7\u5236\u5668\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u76f4\u63a5\u4f5c\u4e3a HTTP \u54cd\u5e94\u4f53\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\uff0c\u4e0d\u518d\u7ecf\u8fc7\u89c6\u56fe\u89e3\u6790\u5668\u8fdb\u884c\u89c6\u56fe\u6e32\u67d3\u3002<br \/>\n\u56e0\u6b64\uff0c\u4f7f\u7528 <code>@RestController<\/code> \u6ce8\u89e3\u7684\u7c7b\u4e2d\u7684\u65b9\u6cd5\u4f1a\u76f4\u63a5\u8fd4\u56de\u6570\u636e\uff0c\u901a\u5e38\u662f JSON \u6216 XML \u683c\u5f0f\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h3>1.1.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<ul>\n<li><strong>\u524d\u540e\u7aef\u5206\u79bb\u5f00\u53d1\uff1a<\/strong> \u5728\u524d\u540e\u7aef\u5206\u79bb\u7684\u9879\u76ee\u4e2d\uff0c\u524d\u7aef\u901a\u5e38\u4f7f\u7528 JavaScript \u6846\u67b6\uff08\u5982 Vue.js\u3001React.js\uff09\uff0c\u540e\u7aef\u8d1f\u8d23\u63d0\u4f9b\u6570\u636e\u63a5\u53e3\u3002<code>@RestController<\/code> \u53ef\u4ee5\u65b9\u4fbf\u5730\u8fd4\u56de JSON \u6570\u636e\u7ed9\u524d\u7aef\uff0c\u6ee1\u8db3\u524d\u540e\u7aef\u6570\u636e\u4ea4\u4e92\u7684\u9700\u6c42\u3002<\/li>\n<li><strong>\u5fae\u670d\u52a1\u67b6\u6784\uff1a<\/strong> \u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\uff0c\u5404\u4e2a\u670d\u52a1\u4e4b\u95f4\u901a\u8fc7 RESTful API \u8fdb\u884c\u901a\u4fe1\u3002<code>@RestController<\/code> \u53ef\u4ee5\u5e2e\u52a9\u5feb\u901f\u6784\u5efa\u670d\u52a1\u7684 API \u63a5\u53e3\uff0c\u5b9e\u73b0\u670d\u52a1\u4e4b\u95f4\u7684\u6570\u636e\u4ea4\u4e92\u3002<\/li>\n<li><strong>\u5f00\u53d1 RESTful API\uff1a<\/strong> \u5f53\u9700\u8981\u5f00\u53d1 RESTful \u98ce\u683c\u7684 API \u65f6\uff0c<code>@RestController<\/code> \u662f\u9996\u9009\u7684\u6ce8\u89e3\uff0c\u5b83\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u7b26\u5408 RESTful \u89c4\u8303\u3002<\/li>\n<\/ul>\n<div style=\"height:1.5em\"><\/div>\n<h3>1.1.3 \u7b80\u5355\u4f7f\u7528\u793a\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 <code>@RestController<\/code> \u521b\u5efa RESTful API \u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-java\">@RestController\npublic class UserController {\n\n    \/\/ \u5904\u7406 HTTP GET \u8bf7\u6c42\uff0c\u8fd4\u56de\u7528\u6237\u5217\u8868\n    @GetMapping(&quot;\/users&quot;)\n    public List&lt;String&gt; getUsers() {\n        return Arrays.asList(&quot;Alice&quot;, &quot;Bob&quot;, &quot;Charlie&quot;);\n    }\n\n    \/\/ \u5904\u7406 HTTP GET \u8bf7\u6c42\uff0c\u6839\u636e ID \u8fd4\u56de\u5355\u4e2a\u7528\u6237\u4fe1\u606f\n    @GetMapping(&quot;\/users\/{id}&quot;)\n    public String getUserById(Long id) {\n        return &quot;User with ID: &quot; + id;\n    }\n}<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>UserController<\/code> \u7c7b\u88ab <code>@RestController<\/code> \u6ce8\u89e3\u6807\u8bb0\uff0c\u5176\u4e2d\u7684 <code>getUsers<\/code> \u65b9\u6cd5\u548c <code>getUserById<\/code> \u65b9\u6cd5\u4f1a\u76f4\u63a5\u5c06\u8fd4\u56de\u503c\u4f5c\u4e3a\u54cd\u5e94\u4f53\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5f53\u5ba2\u6237\u7aef\u53d1\u9001 <code>GET<\/code> \u8bf7\u6c42\u5230 <code>\/users<\/code> \u65f6\uff0c\u4f1a\u8fd4\u56de\u5305\u542b\u7528\u6237\u59d3\u540d\u7684 JSON \u6570\u7ec4\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h3>1.1.4 \u539f\u7406<\/h3>\n<p>\u5f53 Spring \u5e94\u7528\u542f\u52a8\u65f6\uff0cSpring \u6846\u67b6\u4f1a\u626b\u63cf\u5e26\u6709 <code>@RestController<\/code> \u6ce8\u89e3\u7684\u7c7b\uff0c\u5e76\u5c06\u5176\u6ce8\u518c\u4e3a\u63a7\u5236\u5668\u3002<br \/>\n\u5728\u5904\u7406\u8bf7\u6c42\u65f6\uff0cSpring MVC \u4f1a\u6839\u636e\u8bf7\u6c42\u7684 URL \u548c HTTP \u65b9\u6cd5\u5339\u914d\u5230\u76f8\u5e94\u7684\u63a7\u5236\u5668\u65b9\u6cd5\u3002<br \/>\n\u7531\u4e8e <code>@RestController<\/code> \u5305\u542b <code>@ResponseBody<\/code> \u529f\u80fd\uff0c\u63a7\u5236\u5668\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u4f1a\u88ab <code>HttpMessageConverter<\/code> \u8f6c\u6362\u4e3a\u5408\u9002\u7684\u683c\u5f0f\uff08\u5982 JSON\u3001XML \u7b49\uff09\uff0c\u7136\u540e\u76f4\u63a5\u4f5c\u4e3a HTTP \u54cd\u5e94\u4f53\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h3>1.1.5 \u6ce8\u610f\u4e8b\u9879<\/h3>\n<ul>\n<li><strong>\u8fd4\u56de\u503c\u5904\u7406\uff1a<\/strong> <code>@RestController<\/code> \u6ce8\u89e3\u7684\u63a7\u5236\u5668\u65b9\u6cd5\u8fd4\u56de\u503c\u4f1a\u76f4\u63a5\u4f5c\u4e3a\u54cd\u5e94\u4f53\u8fd4\u56de\uff0c\u56e0\u6b64\u8981\u786e\u4fdd\u8fd4\u56de\u503c\u7c7b\u578b\u53ef\u4ee5\u88ab <code>HttpMessageConverter<\/code> \u6b63\u786e\u5904\u7406\u3002\u5982\u679c\u8fd4\u56de\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff0c\u901a\u5e38\u9700\u8981\u786e\u4fdd\u8be5\u5bf9\u8c61\u7684\u5c5e\u6027\u6709\u5bf9\u5e94\u7684 <code>getter<\/code> \u65b9\u6cd5\uff0c\u4ee5\u4fbf\u6b63\u786e\u8f6c\u6362\u4e3a JSON \u6216 XML \u683c\u5f0f\u3002<\/li>\n<li><strong>\u5f02\u5e38\u5904\u7406\uff1a<\/strong> \u7531\u4e8e <code>@RestController<\/code> \u4e3b\u8981\u7528\u4e8e\u8fd4\u56de\u6570\u636e\uff0c\u5f53\u53d1\u751f\u5f02\u5e38\u65f6\uff0c\u9700\u8981\u8fdb\u884c\u5408\u9002\u7684\u5f02\u5e38\u5904\u7406\uff0c\u907f\u514d\u5c06\u5f02\u5e38\u4fe1\u606f\u76f4\u63a5\u66b4\u9732\u7ed9\u5ba2\u6237\u7aef\u3002\u53ef\u4ee5\u4f7f\u7528 <code>@ExceptionHandler<\/code> \u6216 <code>@RestControllerAdvice<\/code> \u6765\u7edf\u4e00\u5904\u7406\u5f02\u5e38\u3002<\/li>\n<li><strong>\u8def\u5f84\u6620\u5c04\uff1a<\/strong> \u8981\u6ce8\u610f\u63a7\u5236\u5668\u65b9\u6cd5\u7684\u8def\u5f84\u6620\u5c04\uff0c\u907f\u514d\u51fa\u73b0\u8def\u5f84\u51b2\u7a81\u3002\u53ef\u4ee5\u4f7f\u7528 <code>@RequestMapping<\/code>\u3001<code>@GetMapping<\/code>\u3001<code>@PostMapping<\/code> \u7b49\u6ce8\u89e3\u6765\u7cbe\u786e\u6307\u5b9a\u8bf7\u6c42\u8def\u5f84\u548c HTTP \u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<div style=\"height:1.5em\"><\/div>\n<h1>\u4e8c\u3001\u8bf7\u6c42\u7c7b\u578b\u76f8\u5173\u6ce8\u89e3<\/h1>\n<h2>2.1 <code>@RequestMapping<\/code><\/h2>\n<p><code>@RequestMapping<\/code> \u662f Spring \u6846\u67b6\u4e2d\u7528\u4e8e\u6620\u5c04 HTTP \u8bf7\u6c42\u5230\u63a7\u5236\u5668\u65b9\u6cd5\u7684\u91cd\u8981\u6ce8\u89e3\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h3>2.1.1 \u4ecb\u7ecd<\/h3>\n<p><code>@RequestMapping<\/code> \u662f\u4e00\u4e2a\u901a\u7528\u7684\u8bf7\u6c42\u6620\u5c04\u6ce8\u89e3\uff0c\u53ef\u7528\u4e8e\u7c7b\u548c\u65b9\u6cd5\u4e0a\u3002<\/p>\n<p>\u5f53\u7528\u4e8e\u7c7b\u4e0a\u65f6\uff0c\u5b83\u4e3a\u8be5\u7c7b\u4e2d\u7684\u6240\u6709\u5904\u7406\u65b9\u6cd5\u8bbe\u7f6e\u4e00\u4e2a\u57fa\u7840\u7684\u8bf7\u6c42\u8def\u5f84\uff1b<br \/>\n\u5f53\u7528\u4e8e\u65b9\u6cd5\u4e0a\u65f6\uff0c\u5b83\u6307\u5b9a\u4e86\u8be5\u65b9\u6cd5\u5177\u4f53\u5904\u7406\u7684\u8bf7\u6c42\u8def\u5f84\u3002<br \/>\n\u901a\u8fc7 <code>@RequestMapping<\/code> \u53ef\u4ee5\u7cbe\u786e\u5730\u5b9a\u4e49\u54ea\u4e9b HTTP \u8bf7\u6c42\u4f1a\u88ab\u54ea\u4e2a\u63a7\u5236\u5668\u65b9\u6cd5\u5904\u7406\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h3>2.1.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<ul>\n<li><strong>\u8bf7\u6c42\u8def\u5f84\u6620\u5c04\uff1a<\/strong> \u5728\u5f00\u53d1 RESTful API \u65f6\uff0c\u9700\u8981\u5c06\u4e0d\u540c\u7684 URL \u8bf7\u6c42\u6620\u5c04\u5230\u76f8\u5e94\u7684\u5904\u7406\u65b9\u6cd5\u4e0a\uff0c<code>@RequestMapping<\/code> \u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u79cd HTTP \u65b9\u6cd5\uff1a<\/strong> \u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e <code>method<\/code> \u5c5e\u6027\u6765\u6307\u5b9a\u5904\u7406\u7684 <code>HTTP<\/code> \u65b9\u6cd5\uff08\u5982 <code>GET<\/code>\u3001<code>POST<\/code>\u3001<code>PUT<\/code>\u3001<code>DELETE<\/code> \u7b49\uff09\uff0c\u6ee1\u8db3\u4e0d\u540c\u4e1a\u52a1\u573a\u666f\u4e0b\u7684\u8bf7\u6c42\u5904\u7406\u9700\u6c42\u3002<\/li>\n<li><strong>\u8bf7\u6c42\u53c2\u6570\u548c\u8bf7\u6c42\u5934\u5339\u914d\uff1a<\/strong> \u53ef\u4ee5\u901a\u8fc7 <code>params<\/code> \u548c <code>headers<\/code> \u5c5e\u6027\u8fdb\u4e00\u6b65\u7ec6\u5316\u8bf7\u6c42\u5339\u914d\u89c4\u5219\uff0c\u53ea\u6709\u5f53\u8bf7\u6c42\u7684\u53c2\u6570\u6216\u8bf7\u6c42\u5934\u6ee1\u8db3\u6307\u5b9a\u6761\u4ef6\u65f6\uff0c\u624d\u4f1a\u8c03\u7528\u76f8\u5e94\u7684\u5904\u7406\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<div style=\"height:1.5em\"><\/div>\n<h3>2.1.3 \u4f7f\u7528\u65b9\u5f0f<\/h3>\n<h4>2.1.3.1 \u7528\u4e8e\u7c7b\u4e0a\u8bbe\u7f6e\u57fa\u7840\u8def\u5f84<\/h4>\n<pre><code class=\"language-java\">@RestController\n@RequestMapping(&quot;\/api&quot;)\npublic class ApiController {\n\n    @RequestMapping(&quot;\/hello&quot;)\n    @ResponseBody\n    public String sayHello() {\n        return &quot;Hello, World!&quot;;\n    }\n}<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>@RequestMapping(&quot;\/api&quot;)<\/code> \u4e3a <code>ApiController<\/code> \u7c7b\u8bbe\u7f6e\u4e86\u57fa\u7840\u8def\u5f84\uff0c<code>sayHello<\/code> \u65b9\u6cd5\u5904\u7406\u7684\u8bf7\u6c42\u8def\u5f84\u662f <code>\/api\/hello<\/code>\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h4>2.1.3.2 \u6307\u5b9a HTTP \u65b9\u6cd5<\/h4>\n<pre><code class=\"language-java\">@RestController\n@RequestMapping(&quot;\/users&quot;)\npublic class UserController {\n\n    \/\/ \u5904\u7406 GET \u8bf7\u6c42\n    @RequestMapping(value = &quot;\/&quot;, method = RequestMethod.GET)\n    @ResponseBody\n    public String getUsers() {\n        return &quot;Get all users&quot;;\n    }\n\n    \/\/ \u5904\u7406 POST \u8bf7\u6c42\n    @RequestMapping(value = &quot;\/&quot;, method = RequestMethod.POST)\n    @ResponseBody\n    public String createUser() {\n        return &quot;Create a new user&quot;;\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u901a\u8fc7 <code>method<\/code> \u5c5e\u6027\u5206\u522b\u6307\u5b9a\u4e86 <code>getUsers<\/code> \u65b9\u6cd5\u5904\u7406 <code>GET<\/code> \u8bf7\u6c42\uff0c<code>createUser<\/code> \u65b9\u6cd5\u5904\u7406 <code>POST<\/code> \u8bf7\u6c42\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h4>2.1.3.3 \u57fa\u4e8e\u8bf7\u6c42\u53c2\u6570\u548c\u8bf7\u6c42\u5934\u5339\u914d<\/h4>\n<pre><code class=\"language-java\">@RestController\n@RequestMapping(&quot;\/products&quot;)\npublic class ProductController {\n\n    \/\/ \u53ea\u6709\u5f53\u8bf7\u6c42\u5305\u542b\u53c2\u6570 &quot;category=electronics&quot; \u65f6\u624d\u4f1a\u5904\u7406\n    @RequestMapping(value = &quot;\/list&quot;, params = &quot;category=electronics&quot;)\n    @ResponseBody\n    public String getElectronicsProducts() {\n        return &quot;List of electronics products&quot;;\n    }\n\n    \/\/ \u53ea\u6709\u5f53\u8bf7\u6c42\u5934\u4e2d\u5305\u542b &quot;X-API-Version: 1.0&quot; \u65f6\u624d\u4f1a\u5904\u7406\n    @RequestMapping(value = &quot;\/details&quot;, headers = &quot;X-API-Version=1.0&quot;)\n    @ResponseBody\n    public String getProductDetails() {\n        return &quot;Product details for API version 1.0&quot;;\n    }\n}<\/code><\/pre>\n<p>\u4f7f\u7528curl\u547d\u4ee4<\/p>\n<pre><code class=\"language-bash\">curl &quot;http:\/\/localhost:8080\/products\/list?category=electronics&quot;<\/code><\/pre>\n<p>\u5f53\u4f60\u6267\u884c\u8fd9\u4e2a\u547d\u4ee4\u65f6\uff0c\u56e0\u4e3a\u8bf7\u6c42\u4e2d\u5305\u542b\u4e86\u53c2\u6570 <code>category=electronics<\/code>\uff0c\u6240\u4ee5\u4f1a\u5339\u914d\u5230 <code>getElectronicsProducts<\/code> \u65b9\u6cd5\uff0c\u6700\u7ec8\u4f1a\u8fd4\u56de <code>&quot;List of electronics products&quot;<\/code>\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h3>2.1.4 \u539f\u7406<\/h3>\n<p>\u5728 Spring \u5e94\u7528\u542f\u52a8\u65f6\uff0cSpring \u6846\u67b6\u4f1a\u626b\u63cf\u6240\u6709\u5e26\u6709 <code>@RequestMapping<\/code> \u6ce8\u89e3\u7684\u7c7b\u548c\u65b9\u6cd5\uff0c\u5e76\u5c06\u8fd9\u4e9b\u6620\u5c04\u4fe1\u606f\u5b58\u50a8\u5728 <code>HandlerMapping<\/code> \u4e2d\u3002<br \/>\n\u5f53\u6709 HTTP \u8bf7\u6c42\u5230\u8fbe\u65f6\uff0cSpring MVC \u4f1a\u6839\u636e\u8bf7\u6c42\u7684 URL\u3001HTTP \u65b9\u6cd5\u3001\u8bf7\u6c42\u53c2\u6570\u548c\u8bf7\u6c42\u5934\u4fe1\u606f\uff0c\u5728 <code>HandlerMapping<\/code> \u4e2d\u67e5\u627e\u5339\u914d\u7684\u5904\u7406\u65b9\u6cd5\uff0c\u7136\u540e\u8c03\u7528\u8be5\u65b9\u6cd5\u5904\u7406\u8bf7\u6c42\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h3>2.1.5 \u6ce8\u610f\u4e8b\u9879<\/h3>\n<ul>\n<li><strong>\u8def\u5f84\u51b2\u7a81\uff1a<\/strong> \u8981\u786e\u4fdd\u4e0d\u540c\u7684\u8bf7\u6c42\u6620\u5c04\u8def\u5f84\u4e0d\u4f1a\u53d1\u751f\u51b2\u7a81\u3002\u5982\u679c\u591a\u4e2a\u65b9\u6cd5\u7684\u8bf7\u6c42\u6620\u5c04\u89c4\u5219\u76f8\u540c\uff0cSpring \u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<li><strong>\u6ce8\u89e3\u7684\u7ec4\u5408\u4f7f\u7528\uff1a<\/strong> \u53ef\u4ee5\u5c06 <code>@RequestMapping<\/code> \u4e0e\u5176\u4ed6\u6ce8\u89e3\uff08\u5982 <code>@PathVariable<\/code>\u3001<code>@RequestParam<\/code>\u3001<code>@RequestBody<\/code> \u7b49\uff09\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u8bf7\u6c42\u53c2\u6570\u3002<\/li>\n<li><strong>\u6027\u80fd\u8003\u8651\uff1a<\/strong> \u8fc7\u591a\u590d\u6742\u7684\u8bf7\u6c42\u6620\u5c04\u89c4\u5219\u53ef\u80fd\u4f1a\u5f71\u54cd\u8bf7\u6c42\u5904\u7406\u7684\u6027\u80fd\uff0c\u56e0\u6b64\u5e94\u5c3d\u91cf\u4fdd\u6301\u8bf7\u6c42\u6620\u5c04\u89c4\u5219\u7684\u7b80\u6d01\u6027\u3002<\/li>\n<\/ul>\n<div style=\"height:1.5em\"><\/div>\n<h2>2.2 <code>@GetMapping<\/code><\/h2>\n<h3>2.2.1 \u4ecb\u7ecd<\/h3>\n<p><code>@GetMapping<\/code> \u662f\u4e00\u4e2a\u7ec4\u5408\u6ce8\u89e3\uff0c\u76f8\u5f53\u4e8e <code>@RequestMapping(method = RequestMethod.GET)<\/code>\u3002<\/p>\n<p>\u7528\u4e8e\u5904\u7406 HTTP GET \u8bf7\u6c42\uff0c\u5b83\u53ef\u4ee5\u5c06 HTTP GET \u8bf7\u6c42\u6620\u5c04\u5230\u7279\u5b9a\u7684\u5904\u7406\u65b9\u6cd5\u4e0a\u3002<\/p>\n<h3>2.2.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<p>\u4e3b\u8981\u7528\u4e8e\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u8d44\u6e90\uff0c\u4f8b\u5982\u83b7\u53d6\u5355\u4e2a\u8d44\u6e90\u3001\u8d44\u6e90\u5217\u8868\u7b49\u3002<\/p>\n<h3>2.2.3 \u4f7f\u7528\u793a\u4f8b<\/h3>\n<pre><code class=\"language-java\">import org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.RestController;\nimport java.util.Arrays;\nimport java.util.List;\n\n@RestController\npublic class ProductController {\n    @GetMapping(&quot;\/products&quot;)\n    public List&lt;String&gt; getAllProducts() {\n        return Arrays.asList(&quot;Product1&quot;, &quot;Product2&quot;, &quot;Product3&quot;);\n    }\n}<\/code><\/pre>\n<h3>2.2.4 \u8bf7\u6c42\u65f6\u7684\u793a\u4f8b<\/h3>\n<p>\u5f53\u5ba2\u6237\u7aef\u53d1\u8d77\u8bf7\u6c42\u65f6\uff0c\u4f7f\u7528\u5982\u4e0b\u5730\u5740\uff1a<\/p>\n<pre><code class=\"language-plaintext\">GET http:\/\/localhost:8080\/products<\/code><\/pre>\n<p>\u670d\u52a1\u5668\u4f1a\u8c03\u7528 <code>getAllProducts<\/code> \u65b9\u6cd5\uff0c\u5e76\u8fd4\u56de\u5305\u542b\u4ea7\u54c1\u540d\u79f0\u7684\u5217\u8868\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h2>2.3 <code>@PostMapping<\/code><\/h2>\n<h3>2.3.1 \u4ecb\u7ecd<\/h3>\n<p><code>@PostMapping<\/code> \u76f8\u5f53\u4e8e <code>@RequestMapping(method = RequestMethod.POST)<\/code>\u3002<\/p>\n<p>\u7528\u4e8e\u5904\u7406 HTTP POST \u8bf7\u6c42\uff0c\u901a\u5e38\u7528\u4e8e\u521b\u5efa\u65b0\u7684\u8d44\u6e90\u3002<\/p>\n<h3>2.3.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<p>\u5f53\u9700\u8981\u5411\u670d\u52a1\u5668\u63d0\u4ea4\u6570\u636e\u4ee5\u521b\u5efa\u65b0\u8d44\u6e90\u65f6\u4f7f\u7528\uff0c\u5982\u521b\u5efa\u7528\u6237\u3001\u521b\u5efa\u8ba2\u5355\u7b49\u3002<\/p>\n<h3>2.3.3 \u7b80\u5355\u4f7f\u7528\u793a\u4f8b<\/h3>\n<p>User\u7c7b\u4ee3\u7801\u6b64\u5904\u7701\u7565\u3002<\/p>\n<p><code>@RequestBody<\/code>\u6ce8\u89e3\u5728 3.3 \u5c0f\u8282\u4f1a\u8bb2\u89e3\u3002<\/p>\n<pre><code class=\"language-java\">@RestController\npublic class UserController {\n    @PostMapping(&quot;\/users&quot;)\n    public String createUser(@RequestBody User user) {\n        return &quot;User created: &quot; + user.getName();\n    }\n}<\/code><\/pre>\n<h3>2.3.4 \u8bf7\u6c42\u65f6\u7684\u793a\u4f8b<\/h3>\n<p>\u5ba2\u6237\u7aef\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u8bf7\u6c42\u521b\u5efa\u4e00\u4e2a\u65b0\u7528\u6237\uff1a<\/p>\n<pre><code class=\"language-plaintext\">POST http:\/\/localhost:8080\/users\nContent-Type: application\/json\n\n{\n    &quot;name&quot;: &quot;John Doe&quot;,\n    &quot;age&quot;: 30\n}<\/code><\/pre>\n<p>\u670d\u52a1\u5668\u4f1a\u5c06\u8bf7\u6c42\u4f53\u4e2d\u7684 JSON \u6570\u636e\u6620\u5c04\u5230 <code>User<\/code> \u5bf9\u8c61\uff0c\u5e76\u8c03\u7528 <code>createUser<\/code> \u65b9\u6cd5\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h2>2.4 <code>@PutMapping<\/code><\/h2>\n<h3>2.4.1 \u4ecb\u7ecd<\/h3>\n<p><code>@PutMapping<\/code> \u76f8\u5f53\u4e8e <code>@RequestMapping(method = RequestMethod.PUT)<\/code>\u3002<\/p>\n<p>\u7528\u4e8e\u5904\u7406 HTTP PUT \u8bf7\u6c42\uff0c\u901a\u5e38\u7528\u4e8e\u66f4\u65b0\u6574\u4e2a\u8d44\u6e90\u3002<\/p>\n<h3>2.4.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<p>\u5f53\u9700\u8981\u66f4\u65b0\u8d44\u6e90\u7684\u6240\u6709\u5c5e\u6027\u65f6\u4f7f\u7528\uff0c\u4f8b\u5982\u66f4\u65b0\u7528\u6237\u7684\u6240\u6709\u4fe1\u606f\u3002<\/p>\n<h3>2.4.3 \u7b80\u5355\u4f7f\u7528\u793a\u4f8b<\/h3>\n<p>User\u7c7b\u4ee3\u7801\u6b64\u5904\u7701\u7565\u3002<\/p>\n<p><code>@PathVariable<\/code>\u6ce8\u89e3\u5728 3.1 \u5c0f\u8282\u4f1a\u8bb2\u89e3\u3002<\/p>\n<pre><code class=\"language-java\">@RestController\npublic class UserController {\n    @PutMapping(&quot;\/users\/{id}&quot;)\n    public String updateUser(@PathVariable Long id, @RequestBody User user) {\n        return &quot;User with ID &quot; + id + &quot; updated: &quot; + user.getName();\n    }\n}<\/code><\/pre>\n<h3>2.4.4 \u8bf7\u6c42\u65f6\u7684\u793a\u4f8b<\/h3>\n<p>\u5ba2\u6237\u7aef\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u8bf7\u6c42\u66f4\u65b0\u7528\u6237\u4fe1\u606f\uff1a<\/p>\n<pre><code class=\"language-plaintext\">PUT http:\/\/localhost:8080\/users\/1\nContent-Type: application\/json\n\n{\n    &quot;name&quot;: &quot;Updated Name&quot;,\n    &quot;age&quot;: 35\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u7684 <code>1<\/code> \u662f\u7528\u6237\u7684 ID\uff0c\u670d\u52a1\u5668\u4f1a\u5c06\u5176\u4f5c\u4e3a <code>@PathVariable<\/code> \u7ed1\u5b9a\u5230 <code>id<\/code> \u53c2\u6570\uff0c\u540c\u65f6\u5c06\u8bf7\u6c42\u4f53\u4e2d\u7684 JSON \u6570\u636e\u6620\u5c04\u5230 <code>User<\/code> \u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528 <code>updateUser<\/code> \u65b9\u6cd5\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h2>2.5 <code>@DeleteMapping<\/code><\/h2>\n<h3>2.5.1 \u4ecb\u7ecd<\/h3>\n<p><code>@DeleteMapping<\/code> \u76f8\u5f53\u4e8e <code>@RequestMapping(method = RequestMethod.DELETE)<\/code>\u3002<\/p>\n<p>\u7528\u4e8e\u5904\u7406 HTTP DELETE \u8bf7\u6c42\uff0c\u901a\u5e38\u7528\u4e8e\u5220\u9664\u8d44\u6e90\u3002<\/p>\n<h3>2.5.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<p>\u5f53\u9700\u8981\u4ece\u670d\u52a1\u5668\u5220\u9664\u67d0\u4e2a\u8d44\u6e90\u65f6\u4f7f\u7528\uff0c\u4f8b\u5982\u5220\u9664\u7528\u6237\u3001\u5220\u9664\u8ba2\u5355\u7b49\u3002<\/p>\n<h3>2.5.3 \u7b80\u5355\u4f7f\u7528\u793a\u4f8b<\/h3>\n<pre><code class=\"language-java\">@RestController\npublic class ProductController {\n    @DeleteMapping(&quot;\/products\/{id}&quot;)\n    public String deleteProduct(@PathVariable Long id) {\n        return &quot;Product with ID &quot; + id + &quot; deleted&quot;;\n    }\n}<\/code><\/pre>\n<h3>2.5.4 \u8bf7\u6c42\u65f6\u7684\u793a\u4f8b<\/h3>\n<p>\u5ba2\u6237\u7aef\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u8bf7\u6c42\u5220\u9664\u6307\u5b9a ID \u7684\u4ea7\u54c1\uff1a<\/p>\n<pre><code class=\"language-plaintext\">DELETE http:\/\/localhost:8080\/products\/2<\/code><\/pre>\n<p>\u8fd9\u91cc\u7684 <code>2<\/code> \u662f\u4ea7\u54c1\u7684 ID\uff0c\u670d\u52a1\u5668\u4f1a\u5c06\u5176\u4f5c\u4e3a <code>@PathVariable<\/code> \u7ed1\u5b9a\u5230 <code>id<\/code> \u53c2\u6570\uff0c\u7136\u540e\u8c03\u7528 <code>deleteProduct<\/code> \u65b9\u6cd5\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h1>\u4e09\u3001\u8bf7\u6c42\u53c2\u6570\u76f8\u5173\u6ce8\u89e3<\/h1>\n<h2>3.1 <code>@PathVariable<\/code><\/h2>\n<h3>3.1.1 \u4ecb\u7ecd<\/h3>\n<p><code>@PathVariable<\/code> \u7528\u4e8e\u4ece URL \u8def\u5f84\u4e2d\u83b7\u53d6\u53d8\u91cf\u503c\u3002<\/p>\n<p>\u5b83\u5141\u8bb8\u5728 URL \u4e2d\u5b9a\u4e49\u53c2\u6570\u5360\u4f4d\u7b26\uff0c\u5e76\u5c06\u5b9e\u9645\u7684\u53c2\u6570\u503c\u7ed1\u5b9a\u5230\u65b9\u6cd5\u7684\u53c2\u6570\u4e0a\u3002<\/p>\n<p><code>@PathVariable<\/code> \u6ce8\u89e3\u7684\u53c2\u6570\u9ed8\u8ba4\u662f\u5fc5\u586b\u7684\u3002<\/p>\n<p>\u5f53\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u8bf7\u6c42 URL \u4e2d\u6ca1\u6709\u5305\u542b\u8be5\u8def\u5f84\u53d8\u91cf\u65f6\uff0cSpring \u4f1a\u5c06\u8be5\u8bf7\u6c42\u89c6\u4e3a\u4e0d\u5339\u914d\u4efb\u4f55\u5904\u7406\u65b9\u6cd5\uff0c\u8fd4\u56de 404 \u72b6\u6001\u7801\u3002\u4e0d\u53ef\u8bbe\u7f6e\u53ef\u9009\uff0c\u662f\u53c2\u6570\u4e5f\u662f\u8bf7\u6c42\u8def\u5f84\uff0c\u6240\u4ee5\u5fc5\u586b\u3002<\/p>\n<h3>3.1.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<p>\u5f53\u9700\u8981\u5728 URL \u4e2d\u4f20\u9012\u53c2\u6570\u65f6\u4f7f\u7528\uff0c\u4f8b\u5982\u6839\u636e\u7528\u6237 ID \u83b7\u53d6\u7528\u6237\u4fe1\u606f\u3001\u6839\u636e\u4ea7\u54c1 ID \u5220\u9664\u4ea7\u54c1\u7b49\u3002<\/p>\n<p>\u5f53\u4f20\u5165\u7684\u53c2\u6570\u540d\u4e0e\u65b9\u6cd5\u7684\u53c2\u6570\u540d\u4e0d\u540c\u65f6\uff0c\u53ef\u4ee5\u5c06\u4f20\u5165\u7684\u53c2\u6570\u540d\u4e0e\u65b9\u6cd5\u53c2\u6570\u540d\u7ed1\u5b9a\uff1a<\/p>\n<pre><code class=\"language-java\">    @GetMapping(&quot;\/{username}&quot;)\n    public String searchByName(@PathVariable(&quot;username&quot;) String name) {\n        return &quot;name:&quot; + name;\n    }<\/code><\/pre>\n<p>\u6216\u8005\u5199\u6210\uff1a<\/p>\n<pre><code class=\"language-java\">    @GetMapping(&quot;\/{username}&quot;)\n    public String searchByName(@PathVariable(value = &quot;username&quot;) String name) {\n        return &quot;name:&quot; + name;\n    }<\/code><\/pre>\n<h3>3.1.3 \u7b80\u5355\u4f7f\u7528\u793a\u4f8b<\/h3>\n<pre><code class=\"language-java\">@RestController\npublic class OrderController {\n    @GetMapping(&quot;\/orders\/{orderId}&quot;)\n    public String getOrder(@PathVariable Long orderId) {\n        return &quot;Order with ID: &quot; + orderId;\n    }\n}<\/code><\/pre>\n<h3>3.1.4 \u8bf7\u6c42\u65f6\u7684\u793a\u4f8b<\/h3>\n<p>\u5ba2\u6237\u7aef\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u8bf7\u6c42\u83b7\u53d6\u6307\u5b9a ID \u7684\u8ba2\u5355\u4fe1\u606f\uff1a<\/p>\n<pre><code class=\"language-plaintext\">GET http:\/\/localhost:8080\/orders\/123<\/code><\/pre>\n<p>\u8fd9\u91cc\u7684 <code>123<\/code> \u662f\u8ba2\u5355\u7684 ID\uff0c\u670d\u52a1\u5668\u4f1a\u5c06\u5176\u4f5c\u4e3a <code>@PathVariable<\/code> \u7ed1\u5b9a\u5230 <code>orderId<\/code> \u53c2\u6570\uff0c\u7136\u540e\u8c03\u7528 <code>getOrder<\/code> \u65b9\u6cd5\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h2>3.2 <code>@RequestParam<\/code><\/h2>\n<h3>3.2.1 \u4ecb\u7ecd<\/h3>\n<p><code>@RequestParam<\/code> \u7528\u4e8e\u4ece URL \u7684\u67e5\u8be2\u53c2\u6570\u4e2d\u83b7\u53d6\u503c\u3002<\/p>\n<p>\u5b83\u53ef\u4ee5\u5c06\u67e5\u8be2\u53c2\u6570\u7ed1\u5b9a\u5230\u65b9\u6cd5\u7684\u53c2\u6570\u4e0a\u3002<\/p>\n<p><code>@RequestParam<\/code> \u6ce8\u89e3\u7684\u53c2\u6570\u9ed8\u8ba4\u662f\u5fc5\u586b\u7684\u3002<\/p>\n<p>\u5f53\u5ba2\u6237\u7aef\u53d1\u9001\u8bf7\u6c42\u65f6\uff0c\u5982\u679c\u6ca1\u6709\u63d0\u4f9b\u8be5\u53c2\u6570\uff0cSpring \u4f1a\u629b\u51fa <code>MissingServletRequestParameterException<\/code> \u5f02\u5e38\u3002<\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u5c06 <code>@RequestParam<\/code> \u7684 <code>required<\/code> \u5c5e\u6027\u8bbe\u7f6e\u4e3a <code>false<\/code> \u6765\u5c06\u53c2\u6570\u53d8\u4e3a\u53ef\u9009\u7684\u3002<\/p>\n<p>\u540c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>defaultValue<\/code> \u5c5e\u6027\u4e3a\u53c2\u6570\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u3002<\/p>\n<h3>3.2.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<p>\u5f53\u9700\u8981\u5728 URL \u4e2d\u4f20\u9012\u7b80\u5355\u7684\u53c2\u6570\u65f6\u4f7f\u7528\uff0c\u4f8b\u5982\u6839\u636e\u7528\u6237\u540d\u641c\u7d22\u7528\u6237\u3001\u6839\u636e\u4ef7\u683c\u8303\u56f4\u7b5b\u9009\u4ea7\u54c1\u7b49\u3002<\/p>\n<p>\u5f53\u4f20\u5165\u7684\u53c2\u6570\u540d\u4e0e\u65b9\u6cd5\u7684\u53c2\u6570\u540d\u4e0d\u540c\u65f6\uff0c\u53ef\u4ee5\u5c06\u4f20\u5165\u7684\u53c2\u6570\u540d\u4e0e\u65b9\u6cd5\u53c2\u6570\u540d\u7ed1\u5b9a\uff1a<\/p>\n<pre><code class=\"language-java\">    @GetMapping(&quot;\/search&quot;)\n    public String search(@RequestParam(&quot;username&quot;) String name) {\n        return &quot;name:&quot; + name;\n    }<\/code><\/pre>\n<p>\u6216\u8005\u5199\u6210\uff1a<\/p>\n<pre><code class=\"language-java\">    @GetMapping(&quot;\/search&quot;)\n    public String search(@RequestParam(value = &quot;username&quot;) String name) {\n        return &quot;name:&quot; + name;\n    }<\/code><\/pre>\n<h3>3.2.3 \u7b80\u5355\u4f7f\u7528\u793a\u4f8b<\/h3>\n<pre><code class=\"language-java\">@RestController\npublic class UserController {\n    @GetMapping(&quot;\/users\/search&quot;)\n    public String searchUsers(@RequestParam String name) {\n        return &quot;Searching for users with name: &quot; + name;\n    }\n}<\/code><\/pre>\n<h3>3.2.4 \u8bf7\u6c42\u65f6\u7684\u793a\u4f8b<\/h3>\n<p>\u5ba2\u6237\u7aef\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u8bf7\u6c42\u641c\u7d22\u6307\u5b9a\u540d\u79f0\u7684\u7528\u6237\uff1a<\/p>\n<pre><code class=\"language-plaintext\">GET http:\/\/localhost:8080\/users\/search?name=John<\/code><\/pre>\n<p>\u670d\u52a1\u5668\u4f1a\u5c06\u67e5\u8be2\u53c2\u6570 <code>name<\/code> \u7684\u503c <code>John<\/code> \u7ed1\u5b9a\u5230 <code>searchUsers<\/code> \u65b9\u6cd5\u7684 <code>name<\/code> \u53c2\u6570\u4e0a\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h2>3.3 <code>@RequestBody<\/code><\/h2>\n<h3>3.3.1 \u4ecb\u7ecd<\/h3>\n<p><code>@RequestBody<\/code> \u7528\u4e8e\u5c06 HTTP \u8bf7\u6c42\u4f53\u7684\u5185\u5bb9\u7ed1\u5b9a\u5230\u65b9\u6cd5\u7684\u53c2\u6570\u4e0a\u3002<\/p>\n<p>\u901a\u5e38\u7528\u4e8e\u5904\u7406 POST \u548c PUT \u8bf7\u6c42\u3002<\/p>\n<p>\u5b83\u4f1a\u6839\u636e\u8bf7\u6c42\u7684 <code>Content-Type<\/code> \u81ea\u52a8\u5c06\u8bf7\u6c42\u4f53\u4e2d\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u5408\u9002\u7684 Java \u5bf9\u8c61\u3002<\/p>\n<h3>3.3.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<p>\u5f53\u9700\u8981\u4f20\u9012\u590d\u6742\u7684\u5bf9\u8c61\u6570\u636e\u65f6\u4f7f\u7528\uff0c\u4f8b\u5982\u521b\u5efa\u7528\u6237\u65f6\u4f20\u9012\u7528\u6237\u7684\u8be6\u7ec6\u4fe1\u606f\u3001\u66f4\u65b0\u4ea7\u54c1\u65f6\u4f20\u9012\u4ea7\u54c1\u7684\u5b8c\u6574\u4fe1\u606f\u7b49\u3002<\/p>\n<h3>3.3.3 \u7b80\u5355\u4f7f\u7528\u793a\u4f8b<\/h3>\n<p>Product\u7c7b\u4ee3\u7801\u7701\u7565\u3002<\/p>\n<pre><code class=\"language-java\">@RestController\npublic class ProductController {\n    @PostMapping(&quot;\/products&quot;)\n    public String createProduct(@RequestBody Product product) {\n        return &quot;Product created: &quot; + product.getName();\n    }\n}<\/code><\/pre>\n<h3>3.3.4 \u8bf7\u6c42\u65f6\u7684\u793a\u4f8b<\/h3>\n<p>\u5ba2\u6237\u7aef\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u8bf7\u6c42\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4ea7\u54c1\uff1a<\/p>\n<pre><code class=\"language-plaintext\">POST http:\/\/localhost:8080\/products\nContent-Type: application\/json\n\n{\n    &quot;name&quot;: &quot;New Product&quot;,\n    &quot;price&quot;: 99.99\n}<\/code><\/pre>\n<p>\u670d\u52a1\u5668\u4f1a\u5c06\u8bf7\u6c42\u4f53\u4e2d\u7684 JSON \u6570\u636e\u6620\u5c04\u5230 <code>Product<\/code> \u5bf9\u8c61\uff0c\u5e76\u8c03\u7528 <code>createProduct<\/code> \u65b9\u6cd5\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h2>3.4 <code>@RequestHeader<\/code><\/h2>\n<h3>3.4.1 \u4ecb\u7ecd<\/h3>\n<p><code>@RequestHeader<\/code> \u6ce8\u89e3\u7528\u4e8e\u5c06 HTTP \u8bf7\u6c42\u5934\u4e2d\u7684\u503c\u7ed1\u5b9a\u5230\u65b9\u6cd5\u7684\u53c2\u6570\u4e0a\u3002<\/p>\n<p>\u901a\u8fc7\u8be5\u6ce8\u89e3\u53ef\u4ee5\u83b7\u53d6\u8bf7\u6c42\u5934\u4e2d\u7684\u7279\u5b9a\u4fe1\u606f\uff0c\u5982 <code>User - Agent<\/code>\u3001<code>Authorization<\/code> \u7b49\u3002<\/p>\n<h3>3.4.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<p>\u5f53\u9700\u8981\u4ece\u8bf7\u6c42\u5934\u4e2d\u83b7\u53d6\u4e00\u4e9b\u5fc5\u8981\u7684\u4fe1\u606f\u65f6\u4f7f\u7528\uff0c\u4f8b\u5982\u83b7\u53d6\u5ba2\u6237\u7aef\u7684\u8eab\u4efd\u9a8c\u8bc1\u4fe1\u606f\u3001\u8bf7\u6c42\u7684\u5185\u5bb9\u7c7b\u578b\u7b49\u3002<\/p>\n<h3>3.4.3 \u7b80\u5355\u4f7f\u7528\u793a\u4f8b<\/h3>\n<pre><code class=\"language-java\">@RestController\npublic class HeaderController {\n    @GetMapping(&quot;\/headers&quot;)\n    public String getHeaderInfo(@RequestHeader(&quot;User - Agent&quot;) String userAgent) {\n        return &quot;User - Agent: &quot; + userAgent;\n    }\n}<\/code><\/pre>\n<h3>3.4.4 \u8bf7\u6c42\u65f6\u7684\u793a\u4f8b<\/h3>\n<p>\u5ba2\u6237\u7aef\u53d1\u8d77\u8bf7\u6c42\u65f6\uff0c\u8bf7\u6c42\u5934\u4e2d\u4f1a\u5305\u542b <code>User - Agent<\/code> \u4fe1\u606f\uff1a<\/p>\n<pre><code class=\"language-plaintext\">GET http:\/\/localhost:8080\/headers\nUser - Agent: Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/91.0.4472.124 Safari\/537.36<\/code><\/pre>\n<p>\u670d\u52a1\u5668\u4f1a\u5c06\u8bf7\u6c42\u5934\u4e2d\u7684 <code>User - Agent<\/code> \u503c\u7ed1\u5b9a\u5230 <code>getHeaderInfo<\/code> \u65b9\u6cd5\u7684 <code>userAgent<\/code> \u53c2\u6570\u4e0a\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h2>3.5 <code>@CookieValue<\/code><\/h2>\n<h3>3.5.1 \u4ecb\u7ecd<\/h3>\n<p><code>@CookieValue<\/code> \u6ce8\u89e3\u7528\u4e8e\u5c06 HTTP \u8bf7\u6c42\u4e2d\u7684 <code>Cookie<\/code> \u503c\u7ed1\u5b9a\u5230\u65b9\u6cd5\u7684\u53c2\u6570\u4e0a\u3002<\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u8be5\u6ce8\u89e3\u83b7\u53d6\u6307\u5b9a\u540d\u79f0\u7684 Cookie \u503c\u3002<\/p>\n<h3>3.5.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<p>\u5f53\u9700\u8981\u4ece\u8bf7\u6c42\u7684 Cookie \u4e2d\u83b7\u53d6\u67d0\u4e9b\u4fe1\u606f\u65f6\u4f7f\u7528\uff0c\u4f8b\u5982\u83b7\u53d6\u7528\u6237\u7684\u4f1a\u8bdd ID\u3001\u7528\u6237\u504f\u597d\u8bbe\u7f6e\u7b49\u3002<\/p>\n<h3>3.5.3 \u7b80\u5355\u4f7f\u7528\u793a\u4f8b<\/h3>\n<pre><code class=\"language-java\">@RestController\npublic class CookieController {\n    @GetMapping(&quot;\/cookies&quot;)\n    public String getCookieValue(@CookieValue(&quot;sessionId&quot;) String sessionId) {\n        return &quot;Session ID: &quot; + sessionId;\n    }\n}<\/code><\/pre>\n<h3>3.5.4 \u8bf7\u6c42\u65f6\u7684\u793a\u4f8b<\/h3>\n<p>\u5ba2\u6237\u7aef\u53d1\u8d77\u8bf7\u6c42\u65f6\uff0c\u8bf7\u6c42\u5934\u4e2d\u5305\u542b Cookie \u4fe1\u606f\uff1a<\/p>\n<pre><code class=\"language-plaintext\">GET http:\/\/localhost:8080\/cookies\nCookie: sessionId=1234567890<\/code><\/pre>\n<p>\u670d\u52a1\u5668\u4f1a\u5c06 Cookie \u4e2d <code>sessionId<\/code> \u7684\u503c\u7ed1\u5b9a\u5230 <code>getCookieValue<\/code> \u65b9\u6cd5\u7684 <code>sessionId<\/code> \u53c2\u6570\u4e0a\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h2>3.6 <code>@MatrixVariable<\/code><\/h2>\n<h3>3.6.1 \u4ecb\u7ecd<\/h3>\n<p><code>@MatrixVariable<\/code> \u6ce8\u89e3\u7528\u4e8e\u4ece URL \u7684\u77e9\u9635\u53d8\u91cf\u4e2d\u83b7\u53d6\u503c\u3002<\/p>\n<p>\u77e9\u9635\u53d8\u91cf\u662f\u4e00\u79cd\u5728 URL \u8def\u5f84\u4e2d\u4f20\u9012\u591a\u4e2a\u53c2\u6570\u7684\u65b9\u5f0f\uff0c\u4f7f\u7528\u5206\u53f7\uff08;\uff09\u5206\u9694\u4e0d\u540c\u7684\u53c2\u6570\u3002<\/p>\n<h3>3.6.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<p>\u5f53\u9700\u8981\u5728 URL \u8def\u5f84\u4e2d\u4f20\u9012\u591a\u4e2a\u76f8\u5173\u53c2\u6570\u65f6\u4f7f\u7528\uff0c\u4f8b\u5982\u5728\u641c\u7d22\u5546\u54c1\u65f6\uff0c\u540c\u65f6\u4f20\u9012\u5546\u54c1\u7684\u989c\u8272\u3001\u5c3a\u5bf8\u7b49\u53c2\u6570\u3002<\/p>\n<h3>3.6.3 \u7b80\u5355\u4f7f\u7528\u793a\u4f8b<\/h3>\n<pre><code class=\"language-java\">@RestController\n@RequestMapping(&quot;\/products&quot;)\npublic class ProductController {\n\n    @GetMapping(&quot;\/{category}\/{productId}&quot;)\n    public String getProduct(@PathVariable String category, @PathVariable String productId,\n                             @MatrixVariable(name = &quot;color&quot;, pathVar = &quot;productId&quot;, required = false) String color,\n                             @MatrixVariable(name = &quot;size&quot;, pathVar = &quot;productId&quot;, required = false) String size) {\n        return &quot;Category: &quot; + category + &quot;, Product ID: &quot; + productId + &quot;, Color: &quot; + color + &quot;, Size: &quot; + size;\n    }\n}<\/code><\/pre>\n<h3>3.6.4 \u8bf7\u6c42\u65f6\u7684\u793a\u4f8b<\/h3>\n<pre><code class=\"language-plaintext\">GET http:\/\/localhost:8080\/products\/clothes\/123;color=red;size=medium<\/code><\/pre>\n<p>\u670d\u52a1\u5668\u4f1a\u4ece URL \u4e2d\u63d0\u53d6\u77e9\u9635\u53d8\u91cf <code>color<\/code> \u548c <code>size<\/code> \u7684\u503c\uff0c\u5e76\u7ed1\u5b9a\u5230\u65b9\u6cd5\u7684\u76f8\u5e94\u53c2\u6570\u4e0a\u3002<\/p>\n<h1>\u56db\u3001\u8bf7\u6c42\u54cd\u5e94\u76f8\u5173\u6ce8\u89e3<\/h1>\n<h2>4.1 <code>@ResponseBody<\/code><\/h2>\n<h3>4.1.1 \u4ecb\u7ecd<\/h3>\n<p><code>@ResponseBody<\/code> \u6ce8\u89e3\u7528\u4e8e\u5c06\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u76f4\u63a5\u4f5c\u4e3a HTTP \u54cd\u5e94\u4f53\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\uff0c\u800c\u4e0d\u662f\u8fdb\u884c\u89c6\u56fe\u89e3\u6790\u3002<\/p>\n<p>\u901a\u5e38\u4e0e <code>@RestController<\/code> \u914d\u5408\u4f7f\u7528\uff08<code>@RestController<\/code> \u76f8\u5f53\u4e8e <code>@Controller<\/code> \u548c <code>@ResponseBody<\/code> \u7684\u7ec4\u5408\uff09\u3002<\/p>\n<h3>4.1.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<p>\u5f53\u9700\u8981\u8fd4\u56de JSON\u3001XML \u7b49\u6570\u636e\u683c\u5f0f\u7ed9\u5ba2\u6237\u7aef\u65f6\u4f7f\u7528\uff0c\u4f8b\u5982\u8fd4\u56de\u7528\u6237\u4fe1\u606f\u5217\u8868\u3001\u4ea7\u54c1\u4fe1\u606f\u7b49\u3002<\/p>\n<h3>4.1.3 \u7b80\u5355\u4f7f\u7528\u793a\u4f8b<\/h3>\n<pre><code class=\"language-java\">@RestController\npublic class ResponseBodyController {\n    @GetMapping(&quot;\/data&quot;)\n    @ResponseBody\n    public List&lt;String&gt; getData() {\n        return Arrays.asList(&quot;Data1&quot;, &quot;Data2&quot;, &quot;Data3&quot;);\n    }\n}<\/code><\/pre>\n<h3>4.1.4 \u8bf7\u6c42\u65f6\u7684\u793a\u4f8b<\/h3>\n<p>\u5ba2\u6237\u7aef\u53d1\u8d77\u8bf7\u6c42\uff1a<\/p>\n<pre><code class=\"language-plaintext\">GET http:\/\/localhost:8080\/data<\/code><\/pre>\n<p>\u670d\u52a1\u5668\u4f1a\u5c06 <code>getData<\/code> \u65b9\u6cd5\u8fd4\u56de\u7684\u5217\u8868\u6570\u636e\u4ee5 JSON \u683c\u5f0f\u76f4\u63a5\u4f5c\u4e3a\u54cd\u5e94\u4f53\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<p>\u5982\u679c\u4e0d\u4f7f\u7528 <code>@ResponseBody<\/code> \uff0c\u63a7\u5236\u5668\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u4f1a\u6839\u636e\u7c7b\u578b\u4e0d\u540c\uff0c\u88ab Spring MVC \u4ee5\u89c6\u56fe\u89e3\u6790\u3001\u643a\u5e26\u6a21\u578b\u6570\u636e\u6e32\u67d3\u89c6\u56fe\u6216\u91cd\u5b9a\u5411\u7b49\u65b9\u5f0f\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h2>4.2 <code>@ResponseStatus<\/code><\/h2>\n<h3>4.2.1 \u4ecb\u7ecd<\/h3>\n<p><code>@ResponseStatus<\/code> \u6ce8\u89e3\u7528\u4e8e\u6307\u5b9a\u65b9\u6cd5\u8fd4\u56de\u65f6\u7684 HTTP \u72b6\u6001\u7801\u3002<\/p>\n<p>\u53ef\u4ee5\u5728\u65b9\u6cd5\u6216\u7c7b\u4e0a\u4f7f\u7528\uff0c\u7528\u4e8e\u63a7\u5236\u54cd\u5e94\u7684\u72b6\u6001\u4fe1\u606f\u3002<\/p>\n<h3>4.2.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<p>\u5f53\u9700\u8981\u81ea\u5b9a\u4e49\u54cd\u5e94\u7684\u72b6\u6001\u7801\u65f6\u4f7f\u7528\u3002<\/p>\n<p>\u4f8b\u5982\u5728\u521b\u5efa\u8d44\u6e90\u6210\u529f\u540e\u8fd4\u56de <code>201 Created<\/code> \u72b6\u6001\u7801\uff0c\u5728\u8d44\u6e90\u4e0d\u5b58\u5728\u65f6\u8fd4\u56de <code>404 Not Found<\/code> \u72b6\u6001\u7801\u7b49\u3002<\/p>\n<h3>4.2.3 \u7b80\u5355\u4f7f\u7528\u793a\u4f8b<\/h3>\n<pre><code class=\"language-java\">@RestController\npublic class StatusController {\n    @GetMapping(&quot;\/status&quot;)\n    @ResponseStatus(HttpStatus.CREATED) \/\/ 201\n    public String createResource() {\n        return &quot;Resource created&quot;;\n    }\n}<\/code><\/pre>\n<h3>4.2.4 \u8bf7\u6c42\u65f6\u7684\u793a\u4f8b<\/h3>\n<p>\u5ba2\u6237\u7aef\u53d1\u8d77\u8bf7\u6c42\uff1a<\/p>\n<pre><code class=\"language-plaintext\">GET http:\/\/localhost:8080\/status<\/code><\/pre>\n<p>\u670d\u52a1\u5668\u4f1a\u8fd4\u56de <code>201 Created<\/code> \u72b6\u6001\u7801\u548c\u54cd\u5e94\u4f53 <code>&quot;Resource created&quot;<\/code>\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h1>\u4e94\u3001\u5f02\u5e38\u5904\u7406\u76f8\u5173\u6ce8\u89e3<\/h1>\n<h2>5.1 <code>@ExceptionHandler<\/code><\/h2>\n<h3>5.1.1 \u4ecb\u7ecd<\/h3>\n<p><code>@ExceptionHandler<\/code> \u6ce8\u89e3\u7528\u4e8e\u5728\u63a7\u5236\u5668\u7c7b\u4e2d\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u65b9\u6cd5\uff0c\u5f53\u63a7\u5236\u5668\u4e2d\u7684\u65b9\u6cd5\u629b\u51fa\u6307\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\u65f6\uff0c\u4f1a\u81ea\u52a8\u8c03\u7528\u8be5\u6ce8\u89e3\u6807\u6ce8\u7684\u65b9\u6cd5\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<h3>5.1.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<p>\u5f53\u9700\u8981\u5bf9\u63a7\u5236\u5668\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u8fdb\u884c\u7edf\u4e00\u5904\u7406\uff0c\u907f\u514d\u5f02\u5e38\u4fe1\u606f\u76f4\u63a5\u66b4\u9732\u7ed9\u5ba2\u6237\u7aef\uff0c\u540c\u65f6\u7ed9\u5ba2\u6237\u7aef\u8fd4\u56de\u53cb\u597d\u7684\u9519\u8bef\u4fe1\u606f\u65f6\u4f7f\u7528\u3002<\/p>\n<h3>5.1.3 \u7b80\u5355\u4f7f\u7528\u793a\u4f8b<\/h3>\n<pre><code class=\"language-java\">@RestController\n@RequestMapping(&quot;\/exception&quot;)\npublic class ExceptionController {\n\n    @GetMapping(&quot;\/error&quot;)\n    public String throwException() {\n        throw new RuntimeException(&quot;Something went wrong!&quot;);\n    }\n\n    @ExceptionHandler(RuntimeException.class)\n    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)\n    public String handleRuntimeException(RuntimeException ex) {\n        return &quot;Error: &quot; + ex.getMessage();\n    }\n}<\/code><\/pre>\n<h3>5.1.4 \u8bf7\u6c42\u65f6\u7684\u793a\u4f8b<\/h3>\n<pre><code class=\"language-plaintext\">GET http:\/\/localhost:8080\/exception\/error<\/code><\/pre>\n<p>\u5f53 <code>throwException<\/code> \u65b9\u6cd5\u629b\u51fa <code>RuntimeException<\/code> \u65f6\uff0c\u4f1a\u81ea\u52a8\u8c03\u7528 <code>handleRuntimeException<\/code> \u65b9\u6cd5\u8fdb\u884c\u5904\u7406\uff0c\u5e76\u8fd4\u56de\u76f8\u5e94\u7684\u9519\u8bef\u4fe1\u606f\u548c <code>500 Internal Server Error<\/code> \u72b6\u6001\u7801\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h2>5.2 <code>@RestControllerAdvice<\/code><\/h2>\n<h3>5.2.1 \u4ecb\u7ecd<\/h3>\n<p><code>@RestControllerAdvice<\/code> \u662f\u4e00\u4e2a\u7ec4\u5408\u6ce8\u89e3\uff0c\u76f8\u5f53\u4e8e <code>@ControllerAdvice<\/code> \u548c <code>@ResponseBody<\/code> \u7684\u7ed3\u5408\u3002<\/p>\n<p>\u5b83\u7528\u4e8e\u5b9a\u4e49\u5168\u5c40\u7684\u5f02\u5e38\u5904\u7406\u7c7b\uff0c\u80fd\u591f\u5904\u7406\u6240\u6709\u63a7\u5236\u5668\u4e2d\u629b\u51fa\u7684\u5f02\u5e38\u3002<\/p>\n<h3>5.2.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<p>\u5f53\u9700\u8981\u5bf9\u6574\u4e2a\u5e94\u7528\u4e2d\u7684\u63a7\u5236\u5668\u5f02\u5e38\u8fdb\u884c\u7edf\u4e00\u5904\u7406\u65f6\u4f7f\u7528\uff0c\u907f\u514d\u5728\u6bcf\u4e2a\u63a7\u5236\u5668\u4e2d\u91cd\u590d\u7f16\u5199\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u3002<\/p>\n<h3>5.2.3 \u7b80\u5355\u4f7f\u7528\u793a\u4f8b<\/h3>\n<pre><code class=\"language-java\">@RestControllerAdvice\npublic class GlobalExceptionHandler {\n\n    @ExceptionHandler(Exception.class)\n    public ResponseEntity&lt;String&gt; handleGlobalException(Exception ex) {\n        return new ResponseEntity&lt;&gt;(&quot;Global Error: &quot; + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);\n    }\n}<\/code><\/pre>\n<h3>5.2.4 \u8bf7\u6c42\u65f6\u7684\u793a\u4f8b<\/h3>\n<p>\u5f53\u4efb\u4f55\u63a7\u5236\u5668\u4e2d\u7684\u65b9\u6cd5\u629b\u51fa <code>Exception<\/code> \u7c7b\u578b\u7684\u5f02\u5e38\u65f6\uff0c\u90fd\u4f1a\u88ab <code>GlobalExceptionHandler<\/code> \u7c7b\u4e2d\u7684 <code>handleGlobalException<\/code> \u65b9\u6cd5\u6355\u83b7\u5e76\u5904\u7406\uff0c\u8fd4\u56de\u7edf\u4e00\u7684\u9519\u8bef\u54cd\u5e94\u3002<\/p>\n<div style=\"height:1.5em\"><\/div>\n<h1>\u516d\u3001\u5176\u4ed6\u6ce8\u89e3<\/h1>\n<h2>6.1 <code>@CrossOrigin<\/code><\/h2>\n<h3>6.1.1 \u4ecb\u7ecd<\/h3>\n<p><code>@CrossOrigin<\/code> \u6ce8\u89e3\u7528\u4e8e\u89e3\u51b3\u8de8\u57df\u8bf7\u6c42\u7684\u95ee\u9898\u3002<\/p>\n<p>\u5728\u524d\u540e\u7aef\u5206\u79bb\u7684\u5f00\u53d1\u4e2d\uff0c\u7531\u4e8e\u6d4f\u89c8\u5668\u7684\u540c\u6e90\u7b56\u7565\uff0c\u4e0d\u540c\u6e90\u7684\u8bf7\u6c42\u4f1a\u53d7\u5230\u9650\u5236\uff0c\u4f7f\u7528\u8be5\u6ce8\u89e3\u53ef\u4ee5\u5141\u8bb8\u8de8\u57df\u8bbf\u95ee\u3002<\/p>\n<h3>6.1.2 \u4f7f\u7528\u573a\u666f<\/h3>\n<p>\u5f53\u524d\u540e\u7aef\u9879\u76ee\u8fd0\u884c\u5728\u4e0d\u540c\u7684\u57df\u540d\u6216\u7aef\u53e3\u65f6\uff0c\u524d\u7aef\u9875\u9762\u5411\u540e\u7aef API \u53d1\u9001\u8bf7\u6c42\u4f1a\u4ea7\u751f\u8de8\u57df\u95ee\u9898\uff0c\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528 <code>@CrossOrigin<\/code> \u6ce8\u89e3\u6765\u89e3\u51b3\u3002<\/p>\n<h3>6.1.3 \u7b80\u5355\u4f7f\u7528\u793a\u4f8b<\/h3>\n<pre><code class=\"language-java\">@RestController\n@CrossOrigin(origins = &quot;https:\/\/www.tqazy.com&quot;)\npublic class CrossOriginController {\n    @GetMapping(&quot;\/cross&quot;)\n    public String crossOriginRequest() {\n        return &quot;Cross - origin request allowed&quot;;\n    }\n}<\/code><\/pre>\n<h3>6.1.4 \u8bf7\u6c42\u65f6\u7684\u793a\u4f8b<\/h3>\n<p>\u524d\u7aef\u9875\u9762\uff08\u8fd0\u884c\u5728 <a href=\"https:\/\/www.tqazy.com\">https:\/\/www.tqazy.com<\/a> \uff09\u53d1\u8d77\u8bf7\u6c42\uff1a<\/p>\n<pre><code class=\"language-plaintext\">GET http:\/\/localhost:8080\/cross<\/code><\/pre>\n<p>\u7531\u4e8e\u5728\u63a7\u5236\u5668\u4e0a\u4f7f\u7528\u4e86 <code>@CrossOrigin<\/code> \u6ce8\u89e3\uff0c\u5141\u8bb8 <a href=\"https:\/\/www.tqazy.com\">https:\/\/www.tqazy.com<\/a> \u57df\u540d\u7684\u8de8\u57df\u8bf7\u6c42\uff0c\u6240\u4ee5\u8be5\u8bf7\u6c42\u53ef\u4ee5\u6b63\u5e38\u8bbf\u95ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Restful API\u98ce\u683c\u63a5\u53e3\u7684\u76f8\u5173\u6ce8\u89e3<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[43],"class_list":["post-1636","post","type-post","status-publish","format-standard","hentry","category-42","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>RESTful API\u98ce\u683c\u7684Controller\u4e2d\u7684\u5e38\u7528\u6ce8\u89e3 | \u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2<\/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.tqazy.com\/?p=1636\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"RESTful API\u98ce\u683c\u7684Controller\u4e2d\u7684\u5e38\u7528\u6ce8\u89e3 | \u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2\" \/>\n<meta property=\"og:description\" content=\"Restful API\u98ce\u683c\u63a5\u53e3\u7684\u76f8\u5173\u6ce8\u89e3\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.tqazy.com\/?p=1636\" \/>\n<meta property=\"og:site_name\" content=\"\u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2\" \/>\n<meta property=\"article:published_time\" content=\"2025-03-13T06:01:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-03-13T08:04:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2024\/05\/20240530090910445.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"\u9676\u5176\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u9676\u5176\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.tqazy.com\/?p=1636#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.tqazy.com\/?p=1636\"},\"author\":{\"name\":\"\u9676\u5176\",\"@id\":\"https:\/\/www.tqazy.com\/#\/schema\/person\/b2e89d56e70f15c261012417c5ef2c4e\"},\"headline\":\"RESTful API\u98ce\u683c\u7684Controller\u4e2d\u7684\u5e38\u7528\u6ce8\u89e3\",\"datePublished\":\"2025-03-13T06:01:25+00:00\",\"dateModified\":\"2025-03-13T08:04:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.tqazy.com\/?p=1636\"},\"wordCount\":122,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.tqazy.com\/#\/schema\/person\/b2e89d56e70f15c261012417c5ef2c4e\"},\"keywords\":[\"\u6ce8\u89e3\"],\"articleSection\":[\"\u6ce8\u89e3\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.tqazy.com\/?p=1636#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.tqazy.com\/?p=1636\",\"url\":\"https:\/\/www.tqazy.com\/?p=1636\",\"name\":\"RESTful API\u98ce\u683c\u7684Controller\u4e2d\u7684\u5e38\u7528\u6ce8\u89e3 | \u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2\",\"isPartOf\":{\"@id\":\"https:\/\/www.tqazy.com\/#website\"},\"datePublished\":\"2025-03-13T06:01:25+00:00\",\"dateModified\":\"2025-03-13T08:04:38+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.tqazy.com\/?p=1636#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.tqazy.com\/?p=1636\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.tqazy.com\/?p=1636#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.tqazy.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"RESTful API\u98ce\u683c\u7684Controller\u4e2d\u7684\u5e38\u7528\u6ce8\u89e3\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.tqazy.com\/#website\",\"url\":\"https:\/\/www.tqazy.com\/\",\"name\":\"\u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2\",\"description\":\"\u5f53\u4e0b\uff0c\u5373\u662f\u672a\u6765\",\"publisher\":{\"@id\":\"https:\/\/www.tqazy.com\/#\/schema\/person\/b2e89d56e70f15c261012417c5ef2c4e\"},\"alternateName\":\"TaoQi's Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.tqazy.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.tqazy.com\/#\/schema\/person\/b2e89d56e70f15c261012417c5ef2c4e\",\"name\":\"\u9676\u5176\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.tqazy.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2024\/06\/20240604021741672.jpg\",\"contentUrl\":\"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2024\/06\/20240604021741672.jpg\",\"width\":940,\"height\":940,\"caption\":\"\u9676\u5176\"},\"logo\":{\"@id\":\"https:\/\/www.tqazy.com\/#\/schema\/person\/image\/\"},\"description\":\"\u4e00\u4e2a\u5f88\u61d2\u7684\u7a0b\u5e8f\u5458\",\"sameAs\":[\"https:\/\/www.tqazy.com\"],\"url\":\"https:\/\/www.tqazy.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"RESTful API\u98ce\u683c\u7684Controller\u4e2d\u7684\u5e38\u7528\u6ce8\u89e3 | \u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2","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.tqazy.com\/?p=1636","og_locale":"zh_CN","og_type":"article","og_title":"RESTful API\u98ce\u683c\u7684Controller\u4e2d\u7684\u5e38\u7528\u6ce8\u89e3 | \u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2","og_description":"Restful API\u98ce\u683c\u63a5\u53e3\u7684\u76f8\u5173\u6ce8\u89e3","og_url":"https:\/\/www.tqazy.com\/?p=1636","og_site_name":"\u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2","article_published_time":"2025-03-13T06:01:25+00:00","article_modified_time":"2025-03-13T08:04:38+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2024\/05\/20240530090910445.jpg","type":"image\/jpeg"}],"author":"\u9676\u5176","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"\u9676\u5176","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.tqazy.com\/?p=1636#article","isPartOf":{"@id":"https:\/\/www.tqazy.com\/?p=1636"},"author":{"name":"\u9676\u5176","@id":"https:\/\/www.tqazy.com\/#\/schema\/person\/b2e89d56e70f15c261012417c5ef2c4e"},"headline":"RESTful API\u98ce\u683c\u7684Controller\u4e2d\u7684\u5e38\u7528\u6ce8\u89e3","datePublished":"2025-03-13T06:01:25+00:00","dateModified":"2025-03-13T08:04:38+00:00","mainEntityOfPage":{"@id":"https:\/\/www.tqazy.com\/?p=1636"},"wordCount":122,"commentCount":0,"publisher":{"@id":"https:\/\/www.tqazy.com\/#\/schema\/person\/b2e89d56e70f15c261012417c5ef2c4e"},"keywords":["\u6ce8\u89e3"],"articleSection":["\u6ce8\u89e3"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.tqazy.com\/?p=1636#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.tqazy.com\/?p=1636","url":"https:\/\/www.tqazy.com\/?p=1636","name":"RESTful API\u98ce\u683c\u7684Controller\u4e2d\u7684\u5e38\u7528\u6ce8\u89e3 | \u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2","isPartOf":{"@id":"https:\/\/www.tqazy.com\/#website"},"datePublished":"2025-03-13T06:01:25+00:00","dateModified":"2025-03-13T08:04:38+00:00","breadcrumb":{"@id":"https:\/\/www.tqazy.com\/?p=1636#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.tqazy.com\/?p=1636"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.tqazy.com\/?p=1636#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.tqazy.com\/"},{"@type":"ListItem","position":2,"name":"RESTful API\u98ce\u683c\u7684Controller\u4e2d\u7684\u5e38\u7528\u6ce8\u89e3"}]},{"@type":"WebSite","@id":"https:\/\/www.tqazy.com\/#website","url":"https:\/\/www.tqazy.com\/","name":"\u9676\u5176\u7684\u4e2a\u4eba\u535a\u5ba2","description":"\u5f53\u4e0b\uff0c\u5373\u662f\u672a\u6765","publisher":{"@id":"https:\/\/www.tqazy.com\/#\/schema\/person\/b2e89d56e70f15c261012417c5ef2c4e"},"alternateName":"TaoQi's Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.tqazy.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"https:\/\/www.tqazy.com\/#\/schema\/person\/b2e89d56e70f15c261012417c5ef2c4e","name":"\u9676\u5176","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.tqazy.com\/#\/schema\/person\/image\/","url":"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2024\/06\/20240604021741672.jpg","contentUrl":"https:\/\/oss.tqazy.com\/wordpress\/autoFile\/2024\/06\/20240604021741672.jpg","width":940,"height":940,"caption":"\u9676\u5176"},"logo":{"@id":"https:\/\/www.tqazy.com\/#\/schema\/person\/image\/"},"description":"\u4e00\u4e2a\u5f88\u61d2\u7684\u7a0b\u5e8f\u5458","sameAs":["https:\/\/www.tqazy.com"],"url":"https:\/\/www.tqazy.com\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/www.tqazy.com\/index.php?rest_route=\/wp\/v2\/posts\/1636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tqazy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tqazy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tqazy.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tqazy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1636"}],"version-history":[{"count":18,"href":"https:\/\/www.tqazy.com\/index.php?rest_route=\/wp\/v2\/posts\/1636\/revisions"}],"predecessor-version":[{"id":1670,"href":"https:\/\/www.tqazy.com\/index.php?rest_route=\/wp\/v2\/posts\/1636\/revisions\/1670"}],"wp:attachment":[{"href":"https:\/\/www.tqazy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tqazy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tqazy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}