{"id":210902,"date":"2021-02-19T08:40:04","date_gmt":"2021-02-19T00:40:04","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=210902"},"modified":"2021-02-02T16:58:13","modified_gmt":"2021-02-02T08:58:13","slug":"swagger3-0","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/swagger3-0.html","title":{"rendered":"Swagger3.0\u65b0\u7248\u5e26\u6765\u7684\u65b0\u53d8\u5316"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728\u793e\u533a\u7684\u63a8\u52a8\u4e0b\uff0cSpringfox3.0 \u53bb\u5e74 7 \u6708\u4efd\u5c31\u53d1\u5e03\u4e86\uff0c\u6700\u8fd1\u7ec8\u4e8e\u5f97\u7a7a\u548c\u5c0f\u4f19\u4f34\u4eec\u804a\u4e00\u804a\u65b0\u7248\u672c\u7684\u65b0\u53d8\u5316\u3002\u8fd9\u6b21\u7684\u7248\u672c\u5347\u7ea7\u4f30\u8ba1\u5c0f\u4f19\u4f34\u4eec\u90fd\u7fd8\u9996\u4ee5\u5f85\u597d\u4e45\u4e86\uff0c\u6bd5\u7adf\u4e0a\u4e00\u6b21\u53d1\u7248\u5df2\u7ecf\u662f\u4e24\u5e74\u524d\u7684\u4e8b\u60c5\u4e86\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\n\u5728\u793e\u533a\u7684\u63a8\u52a8\u4e0b\uff0cSpringfox3.0 \u53bb\u5e74 7 \u6708\u4efd\u5c31\u53d1\u5e03\u4e86\uff0c\u6700\u8fd1\u7ec8\u4e8e\u5f97\u7a7a\u548c\u5c0f\u4f19\u4f34\u4eec\u804a\u4e00\u804a\u65b0\u7248\u672c\u7684\u65b0\u53d8\u5316\u3002\u8fd9\u6b21\u7684\u7248\u672c\u5347\u7ea7\u4f30\u8ba1\u5c0f\u4f19\u4f34\u4eec\u90fd\u7fd8\u9996\u4ee5\u5f85\u597d\u4e45\u4e86\uff0c\u6bd5\u7adf\u4e0a\u4e00\u6b21\u53d1\u7248\u5df2\u7ecf\u662f\u4e24\u5e74\u524d\u7684\u4e8b\u60c5\u4e86\u3002<\/p>\n

\u65b0\u7248\u672c\u8fd8\u662f\u6709\u5f88\u591a\u597d\u73a9\u7684\u5730\u65b9\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u4e0b\u3002<\/p>\n

\u652f\u6301 OpenAPI<\/strong><\/div>\n
\u4ec0\u4e48\u662f OpenAPI?<\/strong><\/span><\/div>\n

OpenAPI \u89c4\u8303\u5176\u5b9e\u5c31\u662f\u4ee5\u524d\u7684 Swagger \u89c4\u8303\uff0c\u5b83\u662f\u4e00\u79cd REST API \u7684\u63cf\u8ff0\u683c\u5f0f\uff0c\u901a\u8fc7\u65e2\u5b9a\u7684\u89c4\u8303\u6765\u63cf\u8ff0\u6587\u6863\u63a5\u53e3\uff0c\u5b83\u662f\u4e1a\u754c\u771f\u6b63\u7684 API \u6587\u6863\u6807\u51c6\uff0c\u53ef\u4ee5\u901a\u8fc7 YAML \u6216\u8005 JSON \u6765\u63cf\u8ff0\u3002\u5b83\u5305\u62ec\u5982\u4e0b\u5185\u5bb9\uff1a<\/p>\n

\u63a5\u53e3(\/users)\u548c\u6bcf\u4e2a\u63a5\u53e3\u7684\u64cd\u4f5c(GET \/users\uff0cPOST \/users)
\n\u8f93\u5165\u53c2\u6570\u548c\u54cd\u5e94\u5185\u5bb9
\n\u8ba4\u8bc1\u65b9\u6cd5
\n\u4e00\u4e9b\u5fc5\u8981\u7684\u8054\u7cfb\u4fe1\u606f\u3001license \u7b49\u3002
\n\u5173\u4e8e OpenAPI \u7684\u66f4\u591a\u5185\u5bb9\uff0c\u611f\u5174\u8da3\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u5728 GitHub \u4e0a\u67e5\u770b\uff1ahttps:\/\/github.com\/OAI\/OpenAPI-Specification\/blob\/master\/versions\/3.0.2.md<\/p>\n

\u4f9d\u8d56<\/strong><\/div>\n

\u4ee5\u524d\u5728\u4f7f\u7528 2.9.2 \u8fd9\u4e2a\u7248\u672c\u7684\u65f6\u5019\uff0c\u4e00\u822c\u6765\u8bf4\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6dfb\u52a0\u5982\u4e0b\u4e24\u4e2a\u4f9d\u8d56\uff1a<\/p>\n

\r\n<dependency>\r\n    <groupId>io.springfox<\/groupId>\r\n    <artifactId>springfox-swagger2<\/artifactId>\r\n    <version>2.9.2<\/version>\r\n<\/dependency>\r\n<dependency>\r\n    <groupId>io.springfox<\/groupId>\r\n    <artifactId>springfox-swagger-ui<\/artifactId>\r\n    <version>2.9.2<\/version>\r\n<\/dependency>\r\n<\/pre>\n

\u8fd9\u4e24\u4e2a\uff0c\u4e00\u4e2a\u7528\u6765\u751f\u6210\u63a5\u53e3\u6587\u6863(JSON \u6570\u636e)\uff0c\u53e6\u4e00\u4e2a\u7528\u6765\u5c55\u793a\u5c06 JSON \u53ef\u89c6\u5316\u3002<\/p>\n

\u5728 3.0 \u7248\u672c\u4e2d\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u8fd9\u4e48\u9ebb\u70e6\u4e86\uff0c\u4e00\u4e2a starter \u5c31\u53ef\u4ee5\u641e\u5b9a\uff1a<\/p>\n

\r\n<dependency> \r\n    <groupid>io.springfox<\/groupid>\r\n    <artifactid>springfox-boot-starter<\/artifactid>\r\n    <version>3.0.0<\/version>\r\n<\/dependency> \r\n<\/pre>\n

\u548c Spring Boot \u4e2d\u7684\u5176\u4ed6 starter \u4e00\u6837\uff0cspringfox-boot-starter \u4f9d\u8d56\u53ef\u4ee5\u5b9e\u73b0\u96f6\u914d\u7f6e\u4ee5\u53ca\u81ea\u52a8\u914d\u7f6e\u652f\u6301\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u5176\u4ed6\u7279\u6b8a\u9700\u6c42\uff0c\u52a0\u4e00\u4e2a\u8fd9\u4e2a\u4f9d\u8d56\u5c31\u884c\u4e86\uff0c\u63a5\u53e3\u6587\u6863\u5c31\u81ea\u52a8\u751f\u6210\u4e86\u3002<\/p>\n

\u63a5\u53e3\u5730\u5740<\/strong><\/div>\n

3.0 \u4e2d\u7684\u63a5\u53e3\u5730\u5740\u4e5f\u548c\u4e4b\u524d\u6709\u6240\u4e0d\u540c\uff0c\u4ee5\u524d\u5728 2.9.2 \u4e2d\u6211\u4eec\u4e3b\u8981\u8bbf\u95ee\u4e24\u4e2a\u5730\u5740\uff1a<\/p>\n

\u6587\u6863\u63a5\u53e3\u5730\u5740\uff1ahttp:\/\/localhost:8080\/v2\/api-docs
\n\u6587\u6863\u9875\u9762\u5730\u5740\uff1ahttp:\/\/localhost:8080\/swagger-ui.html
\n\u73b0\u5728\u5728 3.0 \u4e2d\uff0c\u8fd9\u4e24\u4e2a\u5730\u5740\u4e5f\u53d1\u751f\u4e86\u53d8\u5316\uff1a<\/p>\n

\u6587\u6863\u63a5\u53e3\u5730\u5740\uff1ahttp:\/\/localhost:8080\/v3\/api-docs
\n\u6587\u6863\u9875\u9762\u5730\u5740\uff1ahttp:\/\/localhost:8080\/swagger-ui\/index.html
\n\u7279\u522b\u662f\u6587\u6863\u9875\u9762\u5730\u5740\uff0c\u5982\u679c\u7528\u4e86 3.0\uff0c\u800c\u53bb\u8bbf\u95ee\u4e4b\u524d\u7684\u9875\u9762\uff0c\u4f1a\u62a5 404\u3002<\/p>\n

\u6ce8\u89e3\u65e7\u7684\u6ce8\u89e3\u8fd8\u53ef\u4ee5\u7ee7\u7eed\u4f7f\u7528\uff0c\u4e0d\u8fc7\u5728 3.0 \u4e2d\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5176\u4ed6\u6ce8\u89e3\u3002<\/p>\n

\u4f8b\u5982\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 @EnableOpenApi \u4ee3\u66ff\u4ee5\u524d\u65e7\u7248\u672c\u4e2d\u7684 @EnableSwagger2\u3002<\/p>\n

\u8bdd\u662f\u8fd9\u4e48\u8bf4\uff0c\u4e0d\u8fc7\u677e\u54e5\u5728\u5b9e\u9645\u4f53\u9a8c\u4e2d\uff0c\u611f\u89c9 @EnableOpenApi \u6ce8\u89e3\u7684\u529f\u80fd\u4e0d\u660e\u663e\uff0c\u52a0\u4e0d\u52a0\u90fd\u884c\u3002\u7ffb\u4e86\u4e0b\u6e90\u7801\uff0c@EnableOpenApi \u6ce8\u89e3\u4e3b\u8981\u529f\u80fd\u662f\u4e3a\u4e86\u5bfc\u5165 OpenApiDocumentationConfiguration \u914d\u7f6e\u7c7b\uff0c\u5982\u4e0b\uff1a<\/p>\n

\r\n@Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME) \r\n@Target(value = {java.lang.annotation.ElementType.TYPE}) \r\n@Documented \r\n@Import(OpenApiDocumentationConfiguration.class) \r\npublic @interface EnableOpenApi { \r\n} \r\n<\/pre>\n

\u7136\u540e\u6211\u53c8\u770b\u4e86\u4e0b\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b OpenApiAutoConfiguration\uff0c\u5982\u4e0b\uff1a<\/p>\n

\r\n@Configuration \r\n@EnableConfigurationProperties(SpringfoxConfigurationProperties.class) \r\n@ConditionalOnProperty(value = \"springfox.documentation.enabled\", havingValue = \"true\", matchIfMissing = true) \r\n@Import({ \r\n    OpenApiDocumentationConfiguration.class, \r\n    SpringDataRestConfiguration.class, \r\n    BeanValidatorPluginsConfiguration.class, \r\n    Swagger2DocumentationConfiguration.class, \r\n    SwaggerUiWebFluxConfiguration.class, \r\n    SwaggerUiWebMvcConfiguration.class \r\n}) \r\n@AutoConfigureAfter({ WebMvcAutoConfiguration.class, JacksonAutoConfiguration.class, \r\n    HttpMessageConvertersAutoConfiguration.class, RepositoryRestMvcAutoConfiguration.class }) \r\npublic class OpenApiAutoConfiguration { \r\n \r\n} \r\n<\/pre>\n

\u53ef\u4ee5\u770b\u5230\uff0c\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u91cc\u8fb9\u4e5f\u5bfc\u5165\u4e86 OpenApiDocumentationConfiguration\u3002<\/p>\n

\u6240\u4ee5\u5728\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u5b9e\u9645\u4e0a\u4e0d\u9700\u8981\u6dfb\u52a0 @EnableOpenApi \u6ce8\u89e3\u3002<\/p>\n

\u6839\u636e OpenApiAutoConfiguration \u4e0a\u7684 @ConditionalOnProperty \u6761\u4ef6\u6ce8\u89e3\u4e2d\u7684\u5b9a\u4e49\uff0c\u6211\u4eec\u53d1\u73b0\uff0c\u5982\u679c\u5728 application.properties \u4e2d\u8bbe\u7f6e springfox.documentation.enabled=false\uff0c\u5373\u5173\u95ed\u4e86 swagger \u529f\u80fd\uff0c\u6b64\u65f6\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u5c31\u4e0d\u6267\u884c\u4e86\uff0c\u8fd9\u4e2a\u65f6\u5019\u53ef\u4ee5\u901a\u8fc7 @EnableOpenApi \u6ce8\u89e3\u5bfc\u5165 OpenApiDocumentationConfiguration \u914d\u7f6e\u7c7b\u3002\u6280\u672f\u4e0a\u6765\u8bf4\u903b\u8f91\u662f\u8fd9\u6837\uff0c\u4e0d\u8fc7\u5e94\u7528\u4e2d\u6682\u672a\u53d1\u73b0\u8fd9\u6837\u7684\u9700\u6c42(\u5373\u5728 application.properties \u4e2d\u5173\u95ed swagger\uff0c\u518d\u901a\u8fc7 @EnableOpenApi \u6ce8\u89e3\u5f00\u542f)\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728\u793e\u533a\u7684\u63a8\u52a8\u4e0b\uff0cSpringfox3.0 \u53bb\u5e74 7 \u6708\u4efd\u5c31\u53d1\u5e03\u4e86\uff0c\u6700\u8fd1\u7ec8\u4e8e\u5f97\u7a7a\u548c\u5c0f\u4f19\u4f34\u4eec\u804a\u4e00\u804a\u65b0\u7248\u672c\u7684\u65b0\u53d8\u5316\u3002 […]<\/p>\n","protected":false},"author":1898,"featured_media":210908,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[654],"class_list":["post-210902","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-swagger3-0"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/210902","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=210902"}],"version-history":[{"count":11,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/210902\/revisions"}],"predecessor-version":[{"id":211166,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/210902\/revisions\/211166"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/210908"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=210902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=210902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=210902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}