SpringBoot 2.6.13整合knif4j 3.0.3
于 2023-08-04 16:29:57 首次发布
<div class="operating">
<a class="href-article-edit slide-toggle">版权</a>
</div>
</div>
</div>
</div>
<article class="baidu_pl">
<div id="article_content" class="article_content clearfix">
<link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css"><style class="darkreader darkreader--cors" media="screen">.htmledit_views .kdocs-img,.kdocs-document{max-width:100%;overflow:hidden}.kdocs-document p{line-height:unset!important}.kdocs-checked-list [type=checkbox],.kdocs-unchecked-list [type=checkbox]{margin-right:8px}.kdocs-line-container{margin-bottom:16px}.htmledit_views .kdocs-img{width:100%}.htmledit_views .kdocs-img img{max-height:unset!important}.htmledit_views table.kdocs-table{width:max-content;max-width:unset}.htmledit_views .kdocs-fontSize img.kdocs-latex-img{vertical-align:middle}.htmledit_views .kdocs-document ol,.htmledit_views .kdocs-document ul{margin:0 0 8px}.htmledit_views .kdocs-document ol>li,.htmledit_views .kdocs-document ol>li>p,.htmledit_views .kdocs-document ul>li,.htmledit_views .kdocs-document ul>li>p{margin:0}.htmledit_views .kdocs-document ol+:not(ol),.htmledit_views .kdocs-document ul+:not(ul){padding-top:16px}.htmledit_views .kdocs-document p.kdocs-checked-list,.htmledit_views .kdocs-document p.kdocs-unchecked-list{margin:0 0 8px!important}.htmledit_views .kdocs-document p.kdocs-checked-list+:not(p.kdocs-checked-list):not(p.kdocs-unchecked-list),.htmledit_views .kdocs-document p.kdocs-unchecked-list+:not(p.kdocs-checked-list):not(p.kdocs-unchecked-list){padding-top:24px}.htmledit_views input[type=checkbox]:checked{position:relative}.htmledit_views input[type=checkbox]:checked::after{content:'';position:absolute;left:2px;top:1px;height:6px;width:10px;border-left:1px solid #fff;border-bottom:1px solid #fff;transform:rotate(-45deg)}.htmledit_views input[type=checkbox]:checked::before{content:'';position:absolute;width:13px;height:13px;background:#1989FA;border-radius:2px}</style><style class="darkreader darkreader--sync" media="screen"></style>
<link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-25cebea3f9.css"><style class="darkreader darkreader--cors" media="screen">.htmledit_views address,.htmledit_views cite,.htmledit_views dfn,.htmledit_views em,.htmledit_views i,.htmledit_views span[lang],.htmledit_views var{font-style:italic}.htmledit_views{font-family:-apple-system,SF UI Text,Arial,PingFang SC,Hiragino Sans GB,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif,SimHei,SimSun}.cke_editable{margin:24px;word-wrap:break-word}.htmledit_views figure.right,.htmledit_views img.right{float:right;margin-left:15px;padding:5px}.htmledit_views figure.left,.htmledit_views img.left{float:left;margin-right:15px;padding:5px}.htmledit_views .marker{background-color:#ff0}.htmledit_views figure{text-align:center;margin:0 24px;display:inline-block}.htmledit_views figure>figcaption{text-align:center;margin:8px;color:#999;font-size:14px;display:block}.htmledit_views a>img{padding:1px;margin:1px;border:none;outline:#0782c1 solid 1px}.htmledit_views .code-featured{border:5px solid red}.htmledit_views .math-featured{padding:20px;box-shadow:0 0 2px rgba(200,0,0,1);background-color:rgba(255,0,0,.05);margin:10px}.htmledit_views .image-clean{border:0;background:0 0;padding:0}.htmledit_views .image-clean>figcaption{font-size:.9em;text-align:right}.htmledit_views .image-grayscale{background-color:#fff;color:#666}.htmledit_views .image-grayscale img,.htmledit_views img.image-grayscale{filter:grayscale(100%)}.htmledit_views .embed-240p{max-width:426px;max-height:240px;margin:0 auto}.htmledit_views .embed-360p{max-width:640px;max-height:360px;margin:0 auto}.htmledit_views .embed-480p{max-width:854px;max-height:480px;margin:0 auto}.htmledit_views .embed-720p{max-width:1280px;max-height:720px;margin:0 auto}.htmledit_views .embed-1080p{max-width:1920px;max-height:1080px;margin:0 auto}.htmledit_views p{font-size:18px;color:#4d4d4d;font-weight:400;line-height:24px!important;margin:0 0 16px;overflow-x:auto;overflow-y:hidden}.htmledit_views .img-center,p[align=center]{text-align:center}.htmledit_views img{max-width:100%;height:auto}.htmledit_views strong,.htmledit_views strong span{font-weight:700}.htmledit_views *{box-sizing:border-box}.htmledit_views h1,.htmledit_views h2,.htmledit_views h3,.htmledit_views h4,.htmledit_views h5,.htmledit_views h6{color:#4f4f4f;margin:24px 0 8px;font-weight:600}.htmledit_views ol,.htmledit_views ul{margin:0 0 24px;padding:0;font-size:16px}.htmledit_views ul ol{margin:0 0 24px 32px}.htmledit_views ul li{list-style-type:disc;margin:8px 0 0 32px}.htmledit_views ol li{list-style-type:decimal;margin-left:40px;margin-top:8px}.htmledit_views h1{font-size:22px;line-height:32px}.htmledit_views h2{font-size:20px;line-height:30px}.htmledit_views h3{font-size:18px;line-height:26px}.htmledit_views h4{font-size:16px;line-height:26px}.htmledit_views h5,.htmledit_views h6{font-size:16px;line-height:24px}.htmledit_views blockquote{display:block;padding:16px;margin:0 0 24px;border-left:8px solid #dddfe4;background:#eef0f4;overflow:auto;overflow-scrolling:touch;word-wrap:normal;word-break:normal}.htmledit_views blockquote ol,.htmledit_views blockquote ul{margin-bottom:16px;padding:0;font-size:16px;line-height:24px}.htmledit_views blockquote ol li,.htmledit_views blockquote ul li{margin-bottom:0}.htmledit_views blockquote p{font-size:16px;line-height:26px;font-weight:400;margin-bottom:0;color:#4f4f4f}.htmledit_views hr{margin:24px 0;border:none;border-bottom:solid #ccc 1px}.htmledit_views table tr{border:0;border-top:1px solid #ddd;background-color:#fff}.htmledit_views table{border-collapse:collapse;display:table;width:100%;text-align:left;margin-bottom:24px;margin-left:auto;margin-right:auto}.htmledit_views tbody{border:0}.htmledit_views table tr:nth-child(2n){background-color:#f7f7f7}.htmledit_views table tr td,.htmledit_views table tr th{border:1px solid #ddd;font-size:14px;color:#4f4f4f;line-height:22px;padding:8px;text-align:left}.htmledit_views table tr td p,.htmledit_views table tr th p{font-size:14px;color:#4f4f4f;margin:0;padding:0;text-align:left;line-height:22px}.htmledit_views table tr th p{font-weight:700}.htmledit_views table tr td code,.htmledit_views table tr th code{white-space:normal;word-break:break-word}.htmledit_views table tr th{font-weight:700;background-color:#eff3f5}.htmledit_views dl{margin:24px}.htmledit_views dl dt{margin:8px;font-weight:700}.htmledit_views dl dt dd{margin:8px}.htmledit_views abbr[data-original-title],.htmledit_views abbr[title]{cursor:help;border-bottom:1px dotted #999}.htmledit_views .initialism{font-size:90%;text-transform:uppercase}.htmledit_views pre{white-space:pre-wrap;word-wrap:break-word;margin:0 0 24px;overflow-x:auto;padding:8px}.marker{background-color:#ff0}htmledit_views img.mathcode{margin:0 8px}.htmledit_views kbd,.htmledit_views pre,.htmledit_views samp{font-family:Consolas,Inconsolata,Courier,monospace;font-size:14px;line-height:22px;color:#000}.htmledit_views code ol li div.hljs-ln-code,.htmledit_views code ol li div.hljs-ln-numbers,.htmledit_views pre code,.htmledit_views pre code div,.htmledit_views pre code span{font-family:"Source Code Pro","DejaVu Sans Mono","Ubuntu Mono","Anonymous Pro","Droid Sans Mono",Menlo,Monaco,Consolas,Inconsolata,Courier,monospace,"PingFang SC","Microsoft YaHei",sans-serif}.htmledit_views code{border-radius:4px}.htmledit_views a{color:#4ea1db;text-decoration:none}.htmledit_views a:focus,.htmledit_views a:hover{color:#ca0c16}.htmledit_views a:visited{color:#6795b5}.htmledit_views .hot-keyword:visited{color:#3399ea}.htmledit_views .footnote{vertical-align:top;position:relative;top:-4px;font-size:12px}.htmledit_views .flow-chart,.htmledit_views .sequence-diagram{text-align:center;margin-bottom:24px;font-size:14px!important}.htmledit_views .flow-chart [fill="#000"],.htmledit_views .flow-chart [fill="#000000"],.htmledit_views .flow-chart [fill=black],.htmledit_views .sequence-diagram [fill="#000"],.htmledit_views .sequence-diagram [fill="#000000"],.htmledit_views .sequence-diagram [fill=black]{fill:#4f4f4f}.htmledit_views .flow-chart [stroke="#000000"],.htmledit_views .sequence-diagram [stroke="#000000"]{stroke:#4f4f4f}.htmledit_views .MathJax_SVG_Display{text-align:center;margin:24px 0;font-size:18px;font-weight:400;color:#4f4f4f;position:relative;text-indent:0;max-width:none;max-height:none;min-width:0;min-height:0;width:100%}.htmledit_views pre code{display:block;line-height:22px;overflow-x:auto;white-space:pre;word-wrap:normal;border-radius:4px;padding:8px;tab-size:4}.htmledit_views pre code::-webkit-scrollbar{height:6px;width:138px}.htmledit_views pre code::-webkit-scrollbar-thumb{background:#C2C2C2;border-radius:4px}.htmledit_views pre code code.hljs{padding:0}.htmledit_views pre code:not(.hljs){background-color:#f3f4f5}.htmledit_views pre code,.htmledit_views pre code div,.htmledit_views pre code span{font-size:14px}.htmledit_views .prettyprint,.htmledit_views pre.prettyprint{margin:0 0 24px;padding:8px 16px 4px 56px;background-color:#f6f8fa;border:none}.htmledit_views .prettyprint .pre-numbering{position:absolute;width:48px;background-color:#eef0f4;top:0;left:0;margin:0;padding:8px 0;list-style:none;text-align:right}.htmledit_views .pre-numbering li{padding:0 8px;list-style:none;margin:0}.htmledit_views dl dd{margin:0 0 8px 40px}.htmledit_views kbd{padding:2px 8px;border:1px solid rgba(63,63,63,.25);box-shadow:0 1px 0 rgba(63,63,63,.25);background-color:#fff;color:#333;border-radius:4px;display:inline-block;margin:0 2px;white-space:nowrap}.htmledit_views .dp-highlighter{font-size:12px;text-align:left;margin:0 0 24px}.htmledit_views code ol{margin:0;overflow:hidden}.htmledit_views code ol li{list-style-type:none;margin-left:0;margin-top:0;height:22px}.htmledit_views code ol li div.hljs-ln-code{margin-left:8px}.htmledit_views code ol li div.hljs-ln-code,.htmledit_views code ol li div.hljs-ln-numbers{float:left;height:22px}.htmledit_views code ol li div.hljs-ln-numbers{width:24px;border-right:1px solid #c5c5c5}.htmledit_views code ol li div.hljs-ln-numbers .hljs-ln-line{word-wrap:normal}.htmledit_views code ol.hundred li div.hljs-ln-numbers{width:30px}.htmledit_views code ol.thousand li div.hljs-ln-numbers{width:36px}.htmledit_views .csdn-data-video{width:200px}.htmledit_views .csdn-data-video img{margin:0;display:block;width:200px;height:112px}.htmledit_views .csdn-data-video p{margin-top:10px;margin-bottom:0;font-size:14px}.htmledit_views pre[data-from=code-for-outside]{overflow:hidden}.htmledit_views pre[data-from=code-for-outside] code{overflow-x:auto;overflow-y:hidden}.htmledit_views pre[data-from=code-for-outside] code *{overflow:visible;overflow-wrap:break-word}.htmledit_views pre[data-from=code-for-outside] p code{padding:0}</style><style class="darkreader darkreader--sync" media="screen"></style>
<div id="content_views" class="htmledit_views">
<p>第一步、在pom文件导入knif4j依赖</p>
-
<dependency
>
-
<groupId
>com.github.xiaoymin
<
/groupId
>
-
<artifactId
>knife
4j-spring-boot-starter
<
/artifactId
>
-
<version
>
3.0.3
<
/version
>
-
<
/dependency
>
第二步、配置swagger
-
package com.xmx.config;
-
-
import org.springframework.beans.BeansException;
-
import org.springframework.beans.factory.config.BeanPostProcessor;
-
import org.springframework.context.annotation.Bean;
-
import org.springframework.context.annotation.Configuration;
-
import org.springframework.util.ReflectionUtils;
-
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
-
import springfox.documentation.builders.ApiInfoBuilder;
-
import springfox.documentation.builders.PathSelectors;
-
import springfox.documentation.builders.RequestHandlerSelectors;
-
import springfox.documentation.service.ApiInfo;
-
import springfox.documentation.spi.DocumentationType;
-
import springfox.documentation.spring.web.plugins.Docket;
-
import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider;
-
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;
-
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
-
-
import java.lang.reflect.Field;
-
import java.util.List;
-
import java.util.stream.Collectors;
-
-
@Configuration
-
@EnableSwagger2WebMvc
-
public
class
Knife4jConfig {
-
-
-
@Bean
-
public Docket
createRestApi
() {
-
return
new
Docket(DocumentationType.SWAGGER_2)
-
// apiInfo():配置 API 的一些基本信息,比如:文档标题title,文档描述description,文档版本号version
-
.apiInfo(apiInfo())
-
// select():生成 API 文档的选择器,用于指定要生成哪些 API 文档
-
.select()
-
// apis():指定要生成哪个包下的 API 文档
-
.apis(RequestHandlerSelectors.basePackage(
"com.xmx.controller"))
-
// paths():指定要生成哪个 URL 匹配模式下的 API 文档。这里使用 PathSelectors.any(),表示生成所有的 API 文档。
-
.paths(PathSelectors.any())
-
.build();
-
}
-
private
static
final String API_TILE=
"xx项目";
-
//文档信息配置
-
private ApiInfo
apiInfo
() {
-
return
new
ApiInfoBuilder()
-
// 文档标题
-
.title(API_TILE)
-
// 文档描述信息
-
.description(
"xxx在线API文档")
-
// 文档版本号
-
.version(
"1.0")
-
.build();
-
}
-
@Bean
-
public
static BeanPostProcessor
springfoxHandlerProviderBeanPostProcessor
() {
-
return
new
BeanPostProcessor() {
-
-
@Override
-
public Object
postProcessAfterInitialization
(Object bean, String beanName)
throws BeansException {
-
if (bean
instanceof WebMvcRequestHandlerProvider || bean
instanceof WebFluxRequestHandlerProvider) {
-
customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
-
}
-
return bean;
-
}
-
-
private <T
extends
RequestMappingInfoHandlerMapping>
void
customizeSpringfoxHandlerMappings
(List<T> mappings) {
-
List<T> copy = mappings.stream()
-
.filter(mapping -> mapping.getPatternParser() ==
null)
-
.collect(Collectors.toList());
-
mappings.clear();
-
mappings.addAll(copy);
-
}
-
-
@SuppressWarnings("unchecked")
-
private List<RequestMappingInfoHandlerMapping>
getHandlerMappings
(Object bean) {
-
try {
-
Field
field
= ReflectionUtils.findField(bean.getClass(),
"handlerMappings");
-
field.setAccessible(
true);
-
return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
-
}
catch (IllegalArgumentException | IllegalAccessException e) {
-
throw
new
IllegalStateException(e);
-
}
-
}
-
};
-
}
-
-
-
-
}
-
第三步、在application.yaml文件中配置
spring: mvc: pathmatch: matching-strategy: ant_path_matcher
第四步、访问:http://localhost:8002/doc.html访问文档