{"id":697,"date":"2020-06-14T23:02:37","date_gmt":"2020-06-14T15:02:37","guid":{"rendered":"https:\/\/blog.humh.cn\/?p=697"},"modified":"2020-06-14T23:02:38","modified_gmt":"2020-06-14T15:02:38","slug":"webmvc%e6%8b%a6%e6%88%aa%e5%99%a8%e8%bf%87%e6%bb%a4swagger-ui","status":"publish","type":"post","link":"https:\/\/blog.humh.cn\/?p=697","title":{"rendered":"WebMVC\u62e6\u622a\u5668\u8fc7\u6ee4swagger-ui"},"content":{"rendered":"\n<p>\u9879\u76eecommon-web\u4e2d\uff0cFilterConfig\u4e2d\u6307\u5b9a\u4e86\u62e6\u622a\u5668\uff0c\u5bf9\u63a5\u53e3\u8fdb\u884c\u6743\u9650\uff0c\u7528\u6237\u4fe1\u606f\u7b49\u6821\u9a8c\u3002\u4f1a\u5bf9\u6240\u6709url\u8fdb\u884c\u62e6\u622a\uff0c\u5305\u542bswagger\u7684url\u3002\u4f46\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u4e0d\u5bf9swagger\u62e6\u622a\u3002 <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">registration.excludePathPatterns(whiteListConfig.getUrl())<\/pre>\n\n\n\n<p>\u53ef\u4ee5\u8bbe\u7f6eurl\u767d\u540d\u5355\uff0c\u8fd9\u91cc\u9700\u8981\u5728\u9879\u76ee\u7684\u914d\u7f6e\u6587\u4ef6\uff08yml\u6216properties\uff09\u4e2d\u52a0\u5165\u5982\u4e0b\u914d\u7f6e\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u8bbe\u7f6e\u767d\u540d\u5355\uff0c\u62e6\u622a\u5668\u76f4\u63a5\u653e\u884c\nwhitelist:\n  url:\n    - \"\/swagger-ui.html\"\n    - \"\/webjars\/springfox-swagger-ui\/**\"\n    - \"\/swagger-resources\/**\"\n    - \"\/v2\/api-docs\"<\/pre>\n\n\n\n<p><strong>\u4f46\u662f\u5982\u679c\u767d\u540d\u5355\u53ea\u914d\u7f6e\u201c\/swagger-ui.html\u201d\u7684\u60c5\u51b5\u4e0b\uff0c\u5bf9js\uff0ccss\u7b49\u653e\u884c\uff0c\u6ca1\u6709\u88ab\u62e6\u622a\uff0cpng\u5374\u62e6\u622a\u4e86\uff0c\u5f53\u767d\u540d\u5355\u914d\u7f6e\u201c\/webjars\/springfox-swagger-ui\/**\u201d\u5c31\u53ef\u4ee5\u4e86\uff08\u62e6\u622a\u89c4\u5219\u5f85\u786e\u5b9a\uff0cjs\uff0ccss\u6309\u9053\u7406\u4e5f\u662f\u4f1a\u88ab\u62e6\u622a\u7684\uff09<\/strong> <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-18.png\"><img width=\"1138\" height=\"383\" class=\"wp-image-698 lazyload\" title=\"WebMVC\u62e6\u622a\u5668\u8fc7\u6ee4swagger-ui\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-18.png\" alt=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-18.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-18.png 1138w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-18-300x101.png 300w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-18-1024x345.png 1024w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-18-768x258.png 768w\" sizes=\"(max-width: 1138px) 100vw, 1138px\"><\/a><\/figure><\/div>\n\n\n\n<p> &#8220;v2\/api-docs&#8221;\u4e3aswagger\u5728\u9879\u76ee\u542f\u52a8\u540e\uff0c\u81ea\u52a8\u751f\u6210\u7684json\u683c\u5f0f\u7684rest api\u6587\u6863\uff0c\u5982\u4e0b\u56fe\u6240\u793a <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-19.png\"><img width=\"1204\" height=\"161\" class=\"wp-image-699 lazyload\" title=\"WebMVC\u62e6\u622a\u5668\u8fc7\u6ee4swagger-ui\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-19.png\" alt=\"https:\/\/file.blog.humh.cn\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-19.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-19.png 1204w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-19-300x40.png 300w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-19-1024x137.png 1024w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2020\/06\/d2b5ca33bd970f64a6301fa75ae2eb22-19-768x103.png 768w\" sizes=\"(max-width: 1204px) 100vw, 1204px\"><\/a><\/figure><\/div>\n\n\n\n<p>swagger-ui.html\u5c55\u793a\u7684\u6570\u636e\u6839\u636eapi-docs\u89e3\u6790\u51fa\u6765\u7684\uff0c\u6240\u4ee5\u4e5f\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u653e\u884c\u3002<\/p>\n\n\n\n<p>\u201c\/swagger-resources\u201d\u7528\u6765\u83b7\u53d6api-docs\u7684URI\uff0c\u6240\u4ee5\u4e5f\u653e\u884c\u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u7ee7\u7eed\u901a\u8fc7swagger\u8fdb\u884c\u63a5\u53e3\u8c03\u7528\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff1a\u7ecf\u5b9e\u8df5\uff0c\u62e6\u622a\u5668\u4f1a\u5728\u8d44\u6e90\u6620\u5c04\u4e4b\u524d\uff0c\u4e5f\u5c31\u662f\u8bf4\u62e6\u622a\u5668\u4f1a\u5bf9\u6709url\u62e6\u622a\uff08\u9664\u8fc7\u767d\u540d\u5355\uff09\uff0c\u7136\u540e\u518d\u53bb\u8fdb\u884c\u8d44\u6e90\u6620\u5c04\u3002\u6bd4\u5982\uff0c\u5982\u679c\u62e6\u622a\u5668\u767d\u540d\u5355\u4e2d\u6ca1\u6709\u201c\/static\/**\u201d\u7684\u60c5\u51b5\u4e0b\uff0clocalhost:8080\/static\/app.js \u4e5f\u4f1a\u8fdb\u62e6\u622a\u5668\uff0c\u7136\u540e\u518d\u53bbResourceHandler\u8d44\u6e90\u6620\u5c04\u5339\u914d\u3002\u6240\u4ee5\u6211\u4eec\u7684swagger-ui\u4e00\u5b9a\u9700\u8981\u5148\u914d\u7f6e\u62e6\u622a\u5668\u767d\u540d\u5355\u3002\u7136\u540e\u770b\u60c5\u51b5\u662f\u5426\u9700\u8981\u914d\u7f6e\u8d44\u6e90\u6620\u5c04\u3002\u8be5\u9879\u76ee\u4e2d\u6211\u4eec\u4e0d\u9700\u8981\u5728\u91cd\u5199WebMvcConfigurer\u7684addResourceHandlers\u65b9\u6cd5\u4e2d\u52a0\u5165swagger\u8d44\u6e90\u7684\u914d\u7f6e\u4e86\uff0c\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    @Override\n    public void addResourceHandlers(ResourceHandlerRegistry registry) {\n        registry.addResourceHandler(\"\/static\/**\")\n\n                .addResourceLocations(\"classpath:\/static\/\");\n        \/\/ \u5982\u4e0b\u4e24\u9879\u914d\u7f6e\u53ef\u4ee5\u7701\u53bb\n        registry.addResourceHandler(\"\/swagger-ui.html\")\n                .addResourceLocations(\"classpath:\/META-INF\/resources\/\");\n\n        registry.addResourceHandler(\"\/webjars\/**\")\n                .addResourceLocations(\"classpath:\/META-INF\/resources\/webjars\/\");\n\n    }<\/pre>\n\n\n\n<p>swagger-ui.html\u53ca\u76f8\u5173\u9759\u6001\u8d44\u6e90\u4f4d\u4e8e springfox-swagger-ui jar \u5305\u4e2d\u7684META-INF\/resources\/\u76ee\u5f55\u4e0b\uff0c\u9879\u76ee\u7f16\u8bd1\u540eswagger-ui.html\u5c06\u6dfb\u52a0\u5230classpath\u7684\/META-INF\/resources\/\u4e0b\uff0c\u56e0\u4e3aWebMVC\u9ed8\u8ba4\u914d\u7f6e\u4e86\u201c\/META-INF\/resources\/\u201d\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u4e0a\u8ff0\u90a3\u6837\u518d\u914d\u7f6e\u9759\u6001\u8d44\u6e90\u6620\u5c04\uff08\u5728Servlet3\u534f\u8bae\u89c4\u8303\u4e2d\uff0c\u5305\u542b\u5728JAR\u6587\u4ef6\/META-INFO\/resources\/\u8def\u5f84\u4e0b\u7684\u8d44\u6e90\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\uff0c\u65e0\u9700\u914d\u7f6e\u9759\u6001\u8d44\u6e90\u6620\u5c04\uff09 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9879\u76eecommon-web\u4e2d\uff0cFilterConfig\u4e2d\u6307\u5b9a\u4e86\u62e6\u622a\u5668\uff0c\u5bf9\u63a5\u53e3\u8fdb\u884c\u6743\u9650\uff0c\u7528\u6237\u4fe1\u606f\u7b49\u6821\u9a8c\u3002\u4f1a\u5bf9\u6240\u6709u [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[51],"tags":[35,89],"_links":{"self":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/697"}],"collection":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=697"}],"version-history":[{"count":1,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/697\/revisions"}],"predecessor-version":[{"id":700,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/697\/revisions\/700"}],"wp:attachment":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}