{"id":9540,"date":"2023-11-28T21:05:46","date_gmt":"2023-11-28T13:05:46","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=9540"},"modified":"2023-12-06T17:17:37","modified_gmt":"2023-12-06T09:17:37","slug":"springcloud-zhong-ru-he-she-ji-yi-tao-api-jie-kou","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springcloud-zhong-ru-he-she-ji-yi-tao-api-jie-kou\/","title":{"rendered":"SpringCloud \u4e2d\u5982\u4f55\u8bbe\u8ba1\u4e00\u5957 API \u63a5\u53e3\uff1f"},"content":{"rendered":"<p>\u8bbe\u8ba1\u4e00\u5957\u826f\u597d\u7684 API \u63a5\u53e3\u5bf9\u4e8e\u6784\u5efa\u53ef\u7ef4\u62a4\u3001\u53ef\u6269\u5c55\u3001\u6613\u7528\u7684\u5206\u5e03\u5f0f\u7cfb\u7edf\u548c\u5fae\u670d\u52a1\u67b6\u6784\u81f3\u5173\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u8bbe\u8ba1 Spring Cloud \u4e2d API \u63a5\u53e3\u7684\u4e00\u4e9b\u5efa\u8bae\uff1a<\/p>\n<h3><a id=\"1%E9%81%B5%E5%BE%AA-restful%E8%AE%BE%E8%AE%A1%E5%8E%9F%E5%88%99%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>1. <strong>\u9075\u5faa RESTful \u8bbe\u8ba1\u539f\u5219\uff1a<\/strong><\/h3>\n<ul>\n<li>\u4f7f\u7528\u6807\u51c6\u7684 HTTP \u65b9\u6cd5\uff08GET\u3001POST\u3001PUT\u3001DELETE \u7b49\uff09\u6765\u8868\u793a\u5bf9\u8d44\u6e90\u7684\u64cd\u4f5c\u3002<\/li>\n<li>\u4e3a\u8d44\u6e90\u5b9a\u4e49\u552f\u4e00\u7684\u6807\u8bc6\u7b26\uff08URI\uff09\uff0c\u91c7\u7528\u6e05\u6670\u7684\u3001\u7b26\u5408\u8bed\u4e49\u7684\u8def\u5f84\u7ed3\u6784\u3002<\/li>\n<\/ul>\n<h3><a id=\"2%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>2. <strong>\u7248\u672c\u63a7\u5236\uff1a<\/strong><\/h3>\n<ul>\n<li>\u5728\u8bbe\u8ba1 API \u65f6\uff0c\u8003\u8651\u5f15\u5165\u7248\u672c\u63a7\u5236\uff0c\u907f\u514d\u5bf9\u73b0\u6709\u63a5\u53e3\u505a\u4e0d\u517c\u5bb9\u7684\u66f4\u6539\u65f6\u9020\u6210\u7cfb\u7edf\u7684\u4e0d\u7a33\u5b9a\u3002<\/li>\n<\/ul>\n<h3><a id=\"3%E6%B8%85%E6%99%B0%E7%9A%84%E8%B5%84%E6%BA%90%E5%AE%9A%E4%B9%89%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>3. <strong>\u6e05\u6670\u7684\u8d44\u6e90\u5b9a\u4e49\uff1a<\/strong><\/h3>\n<ul>\n<li>\u5b9a\u4e49\u6e05\u6670\u7684\u8d44\u6e90\u6a21\u578b\uff0c\u6bcf\u4e2a\u8d44\u6e90\u90fd\u6709\u552f\u4e00\u7684\u6807\u8bc6\u7b26\uff0c\u5e76\u4f7f\u7528\u5408\u9002\u7684 HTTP \u65b9\u6cd5\u5bf9\u8d44\u6e90\u8fdb\u884c\u64cd\u4f5c\u3002<\/li>\n<li>\u4f7f\u7528\u5408\u9002\u7684\u72b6\u6001\u7801\uff08HTTP Status Code\uff09\u6765\u8868\u793a\u64cd\u4f5c\u7684\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<h3><a id=\"4%E7%BB%9F%E4%B8%80%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>4. <strong>\u7edf\u4e00\u9519\u8bef\u5904\u7406\uff1a<\/strong><\/h3>\n<ul>\n<li>\u7edf\u4e00\u9519\u8bef\u5904\u7406\uff0c\u4f7f\u7528\u6807\u51c6\u7684\u9519\u8bef\u683c\u5f0f\uff0c\u63d0\u4f9b\u6709\u610f\u4e49\u7684\u9519\u8bef\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 HTTP Status Code 4xx \u8868\u793a\u5ba2\u6237\u7aef\u9519\u8bef\uff0c5xx \u8868\u793a\u670d\u52a1\u5668\u9519\u8bef\u3002<\/li>\n<\/ul>\n<h3><a id=\"5-dto%EF%BC%88%E6%95%B0%E6%8D%AE%E4%BC%A0%E8%BE%93%E5%AF%B9%E8%B1%A1%EF%BC%89%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>5. <strong>DTO\uff08\u6570\u636e\u4f20\u8f93\u5bf9\u8c61\uff09\uff1a<\/strong><\/h3>\n<ul>\n<li>\u4f7f\u7528 DTO \u5c01\u88c5\u6570\u636e\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u63a7\u5236\u4f20\u8f93\u7684\u6570\u636e\u683c\u5f0f\uff0c\u907f\u514d\u5c06\u6570\u636e\u5e93\u5b9e\u4f53\u76f4\u63a5\u66b4\u9732\u7ed9\u5ba2\u6237\u7aef\u3002<\/li>\n<\/ul>\n<h3><a id=\"6%E5%88%86%E9%A1%B5%E4%B8%8E%E8%BF%87%E6%BB%A4%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>6. <strong>\u5206\u9875\u4e0e\u8fc7\u6ee4\uff1a<\/strong><\/h3>\n<ul>\n<li>\u5728\u652f\u6301\u96c6\u5408\u8d44\u6e90\u7684\u63a5\u53e3\u4e2d\uff0c\u8003\u8651\u63d0\u4f9b\u5206\u9875\u548c\u8fc7\u6ee4\u53c2\u6570\uff0c\u4ee5\u4fbf\u5ba2\u6237\u7aef\u80fd\u591f\u66f4\u7075\u6d3b\u5730\u67e5\u8be2\u548c\u5c55\u793a\u6570\u636e\u3002<\/li>\n<\/ul>\n<h3><a id=\"7-hateoas%EF%BC%88%E8%B6%85%E5%AA%92%E4%BD%93%E4%BD%9C%E4%B8%BA%E5%BA%94%E7%94%A8%E7%8A%B6%E6%80%81%E5%BC%95%E6%93%8E%EF%BC%89%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>7. <strong>HATEOAS\uff08\u8d85\u5a92\u4f53\u4f5c\u4e3a\u5e94\u7528\u72b6\u6001\u5f15\u64ce\uff09\uff1a<\/strong><\/h3>\n<ul>\n<li>\u5728\u8fd4\u56de\u8d44\u6e90\u65f6\uff0c\u8003\u8651\u6dfb\u52a0\u76f8\u5173\u8d44\u6e90\u7684\u94fe\u63a5\uff0c\u4ee5\u5e2e\u52a9\u5ba2\u6237\u7aef\u5bfc\u822a\u7cfb\u7edf\u7684\u4e0d\u540c\u90e8\u5206\u3002<\/li>\n<\/ul>\n<h3><a id=\"8%E5%AE%89%E5%85%A8%E6%80%A7%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>8. <strong>\u5b89\u5168\u6027\uff1a<\/strong><\/h3>\n<ul>\n<li>\u4f7f\u7528\u5408\u9002\u7684\u8eab\u4efd\u9a8c\u8bc1\u548c\u6388\u6743\u673a\u5236\uff0c\u786e\u4fdd\u53ea\u6709\u6388\u6743\u7684\u7528\u6237\u53ef\u4ee5\u8bbf\u95ee\u654f\u611f\u8d44\u6e90\u3002<\/li>\n<\/ul>\n<h3><a id=\"9%E6%96%87%E6%A1%A3%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>9. <strong>\u6587\u6863\uff1a<\/strong><\/h3>\n<ul>\n<li>\u63d0\u4f9b\u6e05\u6670\u3001\u8be6\u7ec6\u7684\u6587\u6863\uff0c\u5305\u62ec\u6bcf\u4e2a\u63a5\u53e3\u7684\u7528\u9014\u3001\u8f93\u5165\u53c2\u6570\u3001\u8fd4\u56de\u503c\u3001\u9519\u8bef\u7801\u7b49\u4fe1\u606f\u3002\u4f7f\u7528\u5de5\u5177\uff08\u5982Swagger\uff09\u751f\u6210\u5728\u7ebf\u6587\u6863\u3002<\/li>\n<\/ul>\n<h3><a id=\"10%E5%BC%82%E6%AD%A5%E4%B8%8E%E5%90%8C%E6%AD%A5%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>10. <strong>\u5f02\u6b65\u4e0e\u540c\u6b65\uff1a<\/strong><\/h3>\n<ul>\n<li>\u6839\u636e\u4e1a\u52a1\u9700\u6c42\uff0c\u63d0\u4f9b\u652f\u6301\u540c\u6b65\u548c\u5f02\u6b65\u64cd\u4f5c\u7684\u63a5\u53e3\u3002\u5bf9\u4e8e\u8017\u65f6\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8003\u8651\u5f02\u6b65\u65b9\u5f0f\uff0c\u4ee5\u63d0\u9ad8\u7cfb\u7edf\u7684\u6027\u80fd\u548c\u54cd\u5e94\u901f\u5ea6\u3002<\/li>\n<\/ul>\n<h3><a id=\"11%E7%9B%91%E6%8E%A7%E4%B8%8E%E6%97%A5%E5%BF%97%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>11. <strong>\u76d1\u63a7\u4e0e\u65e5\u5fd7\uff1a<\/strong><\/h3>\n<ul>\n<li>\u5728\u63a5\u53e3\u4e2d\u96c6\u6210\u76d1\u63a7\u548c\u65e5\u5fd7\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u8ffd\u8e2a\u8bf7\u6c42\u548c\u6392\u67e5\u95ee\u9898\u3002\u8003\u8651\u4f7f\u7528 Spring Cloud Sleuth \u548c Zipkin \u5b9e\u73b0\u94fe\u8def\u8ffd\u8e2a\u3002<\/li>\n<\/ul>\n<h3><a id=\"12%E6%B5%8B%E8%AF%95%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>12. <strong>\u6d4b\u8bd5\uff1a<\/strong><\/h3>\n<ul>\n<li>\u7f16\u5199\u5168\u9762\u7684\u5355\u5143\u6d4b\u8bd5\u548c\u96c6\u6210\u6d4b\u8bd5\uff0c\u786e\u4fdd API \u63a5\u53e3\u7684\u7a33\u5b9a\u6027\u548c\u6b63\u786e\u6027\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5efa\u8bae\u90fd\u662f\u57fa\u4e8e\u826f\u597d\u7684 RESTful \u8bbe\u8ba1\u539f\u5219\u548c\u5fae\u670d\u52a1\u67b6\u6784\u7684\u5b9e\u8df5\u7ecf\u9a8c\u3002\u901a\u8fc7\u5408\u7406\u8bbe\u8ba1 API \u63a5\u53e3\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7cfb\u7edf\u7684\u53ef\u7ef4\u62a4\u6027\u3001\u53ef\u6269\u5c55\u6027\uff0c\u5e76\u4e3a\u5f00\u53d1\u4eba\u5458\u63d0\u4f9b\u66f4\u597d\u7684\u5f00\u53d1\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bbe\u8ba1\u4e00\u5957\u826f\u597d\u7684 API \u63a5\u53e3\u5bf9\u4e8e\u6784\u5efa\u53ef\u7ef4\u62a4\u3001\u53ef\u6269\u5c55\u3001\u6613\u7528\u7684\u5206\u5e03\u5f0f\u7cfb\u7edf\u548c\u5fae\u670d\u52a1\u67b6\u6784\u81f3\u5173\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u8bbe\u8ba1 Spri [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117],"tags":[],"class_list":["post-9540","post","type-post","status-publish","format-standard","hentry","category-springcloud"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9540","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=9540"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9540\/revisions"}],"predecessor-version":[{"id":16736,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9540\/revisions\/16736"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=9540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=9540"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=9540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}