{"id":5795,"date":"2022-05-19T18:42:06","date_gmt":"2022-05-19T10:42:06","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=5795"},"modified":"2022-05-19T18:42:06","modified_gmt":"2022-05-19T10:42:06","slug":"swagger%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9fswagger%e6%80%8e%e4%b9%88%e7%94%a8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/5795","title":{"rendered":"Swagger\u662f\u4ec0\u4e48\uff1fSwagger\u600e\u4e48\u7528\uff1f"},"content":{"rendered":"<p><a href=\"http:\/\/c.biancheng.net\/view\/5532.html\">\u539f\u6587\u5730\u5740<\/a><\/p>\n<p>&nbsp;<\/p>\n<div id=\"ad-arc-top\">\n<p class=\"pic\"><a href=\"http:\/\/c.biancheng.net\/view\/9563.html?source=arctop\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/c.biancheng.net\/templets\/new\/images\/index_pc\/jianzhidachang_pc.png?v=1.6.73\" width=\"100%\" \/><\/a><\/p>\n<p class=\"text\"><a href=\"http:\/\/fudao.biancheng.net\/\" target=\"_blank\" rel=\"nofollow noopener\">C\u8bed\u8a00\u4e2d\u6587\u7f51\u63a8\u51fa\u8f85\u5bfc\u73ed\u5566\uff0c\u5305\u62ec\u300cC\u8bed\u8a00\u8f85\u5bfc\u73ed\u3001C++\u8f85\u5bfc\u73ed\u3001\u7b97\u6cd5\/\u6570\u636e\u7ed3\u6784\u8f85\u5bfc\u73ed\u300d\uff0c\u5168\u90e8\u90fd\u662f\u4e00\u5bf9\u4e00\u6559\u5b66\uff1a\u4e00\u5bf9\u4e00\u8f85\u5bfc + \u4e00\u5bf9\u4e00\u7b54\u7591 + \u5e03\u7f6e\u4f5c\u4e1a + \u9879\u76ee\u5b9e\u8df5 + \u6c38\u4e45\u5b66\u4e60\u3002QQ\u5728\u7ebf\uff0c\u968f\u65f6\u54cd\u5e94\uff01<\/a><\/p>\n<\/div>\n<div id=\"arc-body\">Swagger \u662f\u4e00\u4e2a\u89c4\u8303\u4e14\u5b8c\u6574\u7684\u6846\u67b6\uff0c\u7528\u4e8e\u751f\u6210\u3001\u63cf\u8ff0\u3001\u8c03\u7528\u548c\u53ef\u89c6\u5316 RESTful \u98ce\u683c\u7684 Web \u670d\u52a1\u3002<\/p>\n<p>Swagger \u7684\u76ee\u6807\u662f\u5bf9 REST API \u5b9a\u4e49\u4e00\u4e2a\u6807\u51c6\u4e14\u548c\u8bed\u8a00\u65e0\u5173\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u8ba9\u4eba\u548c\u8ba1\u7b97\u673a\u62e5\u6709\u65e0\u987b\u8bbf\u95ee\u6e90\u7801\u3001\u6587\u6863\u6216\u7f51\u7edc\u6d41\u91cf\u76d1\u6d4b\u5c31\u53ef\u4ee5\u53d1\u73b0\u548c\u7406\u89e3\u670d\u52a1\u7684\u80fd\u529b\u3002\u5f53\u901a\u8fc7 Swagger \u8fdb\u884c\u6b63\u786e\u5b9a\u4e49\uff0c\u7528\u6237\u53ef\u4ee5\u7406\u89e3\u8fdc\u7a0b\u670d\u52a1\u5e76\u4f7f\u7528\u6700\u5c11\u5b9e\u73b0\u903b\u8f91\u4e0e\u8fdc\u7a0b\u670d\u52a1\u8fdb\u884c\u4ea4\u4e92\u3002\u4e0e\u4e3a\u5e95\u5c42\u7f16\u7a0b\u6240\u5b9e\u73b0\u7684\u63a5\u53e3\u7c7b\u4f3c\uff0cSwagger \u6d88\u9664\u4e86\u8c03\u7528\u670d\u52a1\u65f6\u53ef\u80fd\u4f1a\u6709\u7684\u731c\u6d4b\u3002<\/p>\n<h4>Swagger \u7684\u4f18\u52bf<\/h4>\n<ul>\n<li>\u652f\u6301 API \u81ea\u52a8\u751f\u6210\u540c\u6b65\u7684\u5728\u7ebf\u6587\u6863\uff1a\u4f7f\u7528 Swagger \u540e\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u4ee3\u7801\u751f\u6210\u6587\u6863\uff0c\u4e0d\u518d\u9700\u8981\u81ea\u5df1\u624b\u52a8\u7f16\u5199\u63a5\u53e3\u6587\u6863\u4e86\uff0c\u5bf9\u7a0b\u5e8f\u5458\u6765\u8bf4\u975e\u5e38\u65b9\u4fbf\uff0c\u53ef\u4ee5\u8282\u7ea6\u5199\u6587\u6863\u7684\u65f6\u95f4\u53bb\u5b66\u4e60\u65b0\u6280\u672f\u3002<\/li>\n<li>\u63d0\u4f9b Web \u9875\u9762\u5728\u7ebf\u6d4b\u8bd5 API\uff1a\u5149\u6709\u6587\u6863\u8fd8\u4e0d\u591f\uff0cSwagger \u751f\u6210\u7684\u6587\u6863\u8fd8\u652f\u6301\u5728\u7ebf\u6d4b\u8bd5\u3002\u53c2\u6570\u548c\u683c\u5f0f\u90fd\u5b9a\u597d\u4e86\uff0c\u76f4\u63a5\u5728\u754c\u9762\u4e0a\u8f93\u5165\u53c2\u6570\u5bf9\u5e94\u7684\u503c\u5373\u53ef\u5728\u7ebf\u6d4b\u8bd5\u63a5\u53e3\u3002<\/li>\n<\/ul>\n<h2>\u96c6\u6210 Swagger \u7ba1\u7406 API \u6587\u6863<\/h2>\n<h4>1\uff09\u9879\u76ee\u4e2d\u96c6\u6210 Swagger<\/h4>\n<p>\u96c6\u6210 Swagger \u6211\u4eec\u4f7f\u7528\u5c01\u88c5\u597d\u4e86\u7684 Starter \u5305\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002<\/p>\n<p class=\"info-box\">&lt;!&#8211; Swagger &#8211;&gt;<br \/>\n&lt;dependency&gt;<br \/>\n&lt;groupId&gt;com.spring4all&lt;\/groupId&gt;<br \/>\n&lt;artifactId&gt;swagger-spring-boot-starter&lt;\/artifactId&gt;<br \/>\n&lt;version&gt;1.7.1.RELEASE&lt;\/version&gt;<br \/>\n&lt;\/dependency&gt;<\/p>\n<p>\u5728\u542f\u52a8\u7c7b\u4e2d\u4f7f\u7528 @EnableSwagger2Doc \u5f00\u542f Swagger\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002<\/p>\n<div class=\"snippet-container\">\n<div class=\"sh_bright snippet-wrap\">\n<ol class=\"snippet-num\">\n<li>@EnableSwagger2Doc<\/li>\n<li>@<a class=\"sh_url\" href=\"http:\/\/c.biancheng.net\/spring_boot\/\" target=\"_blank\" rel=\"noopener\">SpringBoot<\/a>Application<\/li>\n<li><span class=\"sh_keyword\">public<\/span> <span class=\"sh_keyword\">class<\/span> <span class=\"sh_classname\">AuthApplication<\/span> <span class=\"sh_cbracket\">{<\/span><\/li>\n<li><span class=\"sh_keyword\">public<\/span> <span class=\"sh_keyword\">static<\/span> <span class=\"sh_type\">void<\/span> <span class=\"sh_function\">main<\/span><span class=\"sh_symbol\">(<\/span>String<span class=\"sh_symbol\">[]<\/span> args<span class=\"sh_symbol\">)<\/span> <span class=\"sh_cbracket\">{<\/span><\/li>\n<li><a class=\"sh_url\" href=\"http:\/\/c.biancheng.net\/spring\/\" target=\"_blank\" rel=\"noopener\">Spring<\/a>Application<span class=\"sh_symbol\">.<\/span><span class=\"sh_function\">run<\/span><span class=\"sh_symbol\">(<\/span>AuthApplication<span class=\"sh_symbol\">.<\/span><span class=\"sh_keyword\">class<\/span><span class=\"sh_symbol\">,<\/span> args<span class=\"sh_symbol\">);<\/span><\/li>\n<li><span class=\"sh_cbracket\">}<\/span><\/li>\n<li><span class=\"sh_cbracket\">}<\/span><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<h4>2\uff09\u4f7f\u7528 Swagger \u751f\u6210\u6587\u6863<\/h4>\n<p>Swagger \u662f\u901a\u8fc7\u6ce8\u89e3\u7684\u65b9\u5f0f\u6765\u751f\u6210\u5bf9\u5e94\u7684 API\uff0c\u5728\u63a5\u53e3\u4e0a\u6211\u4eec\u9700\u8981\u52a0\u4e0a\u5404\u79cd\u6ce8\u89e3\u6765\u63cf\u8ff0\u8fd9\u4e2a\u63a5\u53e3\uff0c\u5173\u4e8e Swagger \u6ce8\u89e3\u7684\u4f7f\u7528\u5728\u6559\u7a0b\u540e\u9762\u4f1a\u6709\u8be6\u7ec6\u8bb2\u89e3\uff0c\u672c\u8282\u53ea\u662f\u5e26\u5927\u5bb6\u5feb\u901f\u4f7f\u7528 Swagger\uff0c\u4f7f\u7528\u65b9\u6cd5\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002<\/p>\n<div class=\"snippet-container\">\n<div class=\"sh_bright snippet-wrap\">\n<ol class=\"snippet-num\">\n<li>@<span class=\"sh_function\">ApiOperation<\/span><span class=\"sh_symbol\">(<\/span>value <span class=\"sh_symbol\">=<\/span> <span class=\"sh_string\">&#8220;\u65b0\u589e\u7528\u6237&#8221;<\/span><span class=\"sh_symbol\">)<\/span><\/li>\n<li>@<span class=\"sh_function\">ApiResponses<\/span><span class=\"sh_symbol\">(<\/span><span class=\"sh_cbracket\">{<\/span> @<span class=\"sh_function\">ApiResponse<\/span><span class=\"sh_symbol\">(<\/span>code <span class=\"sh_symbol\">=<\/span> <span class=\"sh_number\">200<\/span><span class=\"sh_symbol\">,<\/span> message <span class=\"sh_symbol\">=<\/span> <span class=\"sh_string\">&#8220;OK&#8221;<\/span><span class=\"sh_symbol\">,<\/span> response <span class=\"sh_symbol\">=<\/span> UserDto<span class=\"sh_symbol\">.<\/span><span class=\"sh_keyword\">class<\/span><span class=\"sh_symbol\">)<\/span> <span class=\"sh_cbracket\">}<\/span><span class=\"sh_symbol\">)<\/span><\/li>\n<li>@<span class=\"sh_function\">PostMapping<\/span><span class=\"sh_symbol\">(<\/span><span class=\"sh_string\">&#8220;\/user&#8221;<\/span><span class=\"sh_symbol\">)<\/span><\/li>\n<li><span class=\"sh_keyword\">public<\/span> <span class=\"sh_usertype\">UserDto<\/span> <span class=\"sh_function\">addUser<\/span><span class=\"sh_symbol\">(<\/span>@RequestBody <span class=\"sh_usertype\">AddUserParam<\/span> param<span class=\"sh_symbol\">)<\/span> <span class=\"sh_cbracket\">{<\/span><\/li>\n<li>System<span class=\"sh_symbol\">.<\/span>err<span class=\"sh_symbol\">.<\/span><span class=\"sh_function\">println<\/span><span class=\"sh_symbol\">(<\/span>param<span class=\"sh_symbol\">.<\/span><span class=\"sh_function\">getName<\/span><span class=\"sh_symbol\">());<\/span><\/li>\n<li><span class=\"sh_keyword\">return<\/span> <span class=\"sh_keyword\">new<\/span> <span class=\"sh_function\">UserDto<\/span><span class=\"sh_symbol\">();<\/span><\/li>\n<li><span class=\"sh_cbracket\">}<\/span><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<p>\u53c2\u6570\u7c7b\u5b9a\u4e49\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002<\/p>\n<div class=\"snippet-container\">\n<div class=\"sh_bright snippet-wrap\">\n<ol class=\"snippet-num\">\n<li>@Data<\/li>\n<li>@<span class=\"sh_function\">ApiModel<\/span><span class=\"sh_symbol\">(<\/span>value <span class=\"sh_symbol\">=<\/span> <span class=\"sh_string\">&#8220;com.biancheng.auth.param.AddUserParam&#8221;<\/span><span class=\"sh_symbol\">,<\/span> description <span class=\"sh_symbol\">=<\/span> <span class=\"sh_string\">&#8220;\u65b0\u589e\u7528\u6237\u53c2\u6570&#8221;<\/span><span class=\"sh_symbol\">)<\/span><\/li>\n<li><span class=\"sh_keyword\">public<\/span> <span class=\"sh_keyword\">class<\/span> <span class=\"sh_classname\">AddUserParam<\/span> <span class=\"sh_cbracket\">{<\/span><\/li>\n<li><\/li>\n<li>@<span class=\"sh_function\">ApiModelProperty<\/span><span class=\"sh_symbol\">(<\/span>value <span class=\"sh_symbol\">=<\/span> <span class=\"sh_string\">&#8220;ID&#8221;<\/span><span class=\"sh_symbol\">)<\/span><\/li>\n<li><span class=\"sh_keyword\">private<\/span> <span class=\"sh_usertype\">String<\/span> id<span class=\"sh_symbol\">;<\/span><\/li>\n<li><\/li>\n<li>@<span class=\"sh_function\">ApiModelProperty<\/span><span class=\"sh_symbol\">(<\/span>value <span class=\"sh_symbol\">=<\/span> <span class=\"sh_string\">&#8220;\u540d\u79f0&#8221;<\/span><span class=\"sh_symbol\">)<\/span><\/li>\n<li><span class=\"sh_keyword\">private<\/span> <span class=\"sh_usertype\">String<\/span> name<span class=\"sh_symbol\">;<\/span><\/li>\n<li><\/li>\n<li>@<span class=\"sh_function\">ApiModelProperty<\/span><span class=\"sh_symbol\">(<\/span>value <span class=\"sh_symbol\">=<\/span> <span class=\"sh_string\">&#8220;\u5e74\u9f84&#8221;<\/span><span class=\"sh_symbol\">)<\/span><\/li>\n<li><span class=\"sh_keyword\">private<\/span> <span class=\"sh_type\">int<\/span> age<span class=\"sh_symbol\">;<\/span><\/li>\n<li><span class=\"sh_cbracket\">}<\/span><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<h2>\u5728\u7ebf\u6d4b\u8bd5\u63a5\u53e3<\/h2>\n<p>\u63a5\u53e3\u67e5\u770b\u5730\u5740\u53ef\u4ee5\u901a\u8fc7\u670d\u52a1\u5730\u5740 \/swagger-ui.html \u6765\u8bbf\u95ee\uff0c\u89c1\u56fe 1\u3002<\/p>\n<div><img decoding=\"async\" src=\"http:\/\/c.biancheng.net\/uploads\/allimg\/190830\/5-1ZS0115025A8.png\" alt=\"swagger\u4e3b\u9875\" \/><br \/>\n\u56fe 1\u00a0\u00a0swagger \u4e3b\u9875<\/div>\n<p>\u53ef\u4ee5\u5c55\u5f00\u770b\u8be6\u60c5\uff0c\u89c1\u56fe 2\u3002<\/p>\n<div><img decoding=\"async\" src=\"http:\/\/c.biancheng.net\/uploads\/allimg\/190830\/5-1ZS01153045a.png\" alt=\"swagger\u63a5\u53e3\u4e3b\u9875\" \/><br \/>\n\u56fe 2\u00a0\u00a0swagger \u63a5\u53e3\u4e3b\u9875<\/div>\n<p>\u5728 param \u4e2d\u8f93\u5165\u53c2\u6570\uff0c\u70b9\u51fb Try it out \u6309\u94ae\u53ef\u4ee5\u8c03\u7528\u63a5\u53e3\uff0c\u89c1\u56fe 3\u3002<\/p>\n<div><img decoding=\"async\" src=\"http:\/\/c.biancheng.net\/uploads\/allimg\/190830\/5-1ZS0120RY03.png\" alt=\"swagger\u63a5\u53e3\u8c03\u7528\u7ed3\u679c\" \/><br \/>\n\u56fe 3\u00a0\u00a0swagger \u63a5\u53e3\u8c03\u7528\u7ed3\u679c<\/div>\n<\/div>\n<div id=\"arc-append\">\n<p>\u5173\u6ce8\u5fae\u4fe1\u516c\u4f17\u53f7\u300c\u7ad9\u957f\u4e25\u957f\u751f\u300d\uff0c\u5728\u624b\u673a\u4e0a\u9605\u8bfb\u6240\u6709\u6559\u7a0b\uff0c\u968f\u65f6\u968f\u5730\u90fd\u80fd\u5b66\u4e60\u3002\u672c\u516c\u4f17\u53f7\u7531<a class=\"col-link\" href=\"http:\/\/c.biancheng.net\/view\/8092.html\" target=\"_blank\" rel=\"nofollow noopener\">C\u8bed\u8a00\u4e2d\u6587\u7f51\u7ad9\u957f<\/a>\u8fd0\u8425\uff0c\u6bcf\u65e5\u66f4\u65b0\uff0c\u575a\u6301\u539f\u521b\uff0c\u6562\u8bf4\u771f\u8bdd\uff0c\u51e1\u4e8b\u6709\u6001\u5ea6\u3002<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 &nbsp; C\u8bed\u8a00\u4e2d\u6587\u7f51\u63a8\u51fa<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[24],"tags":[121],"class_list":["post-5795","post","type-post","status-publish","format-standard","hentry","category-value_docs","tag-05-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5795","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=5795"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5795\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=5795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=5795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=5795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}