赞
踩
首先介绍一下Knife4j. 就是一款接口文档框架,跟swagger类似。 但是整合了很多swagger的功能,页面比swagger美观。现在大有取代swagger之势
官方文档地址: https://doc.xiaominfo.com/docs/quick-start
其实主要的集成方式,在文档里都已经描述了,并且我之前也写过集成SpringBoot集成swagger的文章,大同小异。我用的是SpringBoot2. 没用3是因为没装JDK17. 都这个阶段了,还是建议用3.
在唠叨一下,knife4j 对与openapi2和openapi3都支持,我这里选用的是openapi3.
开始集成,老样子,先引入依赖:
- <!-- 接口文档 -->
- <dependency>
- <groupId>com.github.xiaoymin</groupId>
- <artifactId>knife4j-openapi3-spring-boot-starter</artifactId>
- <version>4.4.0</version>
- </dependency>
然后在配置文件里添加配置:
knife4j: enable: true openapi: title: 接口文档 description: "接口文档生成" email: "" concat: admin url: https://docs.xiaominfo.com version: v4.0 license: Apache 2.0 license-url: https://stackoverflow.com/ terms-of-service-url: https://stackoverflow.com/ group: test1: group-name: ai聊天室 api-rule: package api-rule-resources: - comn.xxx.xx.cms
我这个配置的好像不太对, 但是也无关紧要,能处理。 详细配置可以参考官网上有个gitee上的代码。
然后就是写注解了,需要在Controller上和vo上写注解,注意openapi2和openapi3的注解是不一样的。这里就简单给个例子吧:
- @RestController
- @RequestMapping("/api/faqType")
- @Tag(name="FAQ类型相关接口")
- public class FaqTypeController {
-
- private final FaqTypeBiz faqTypeBiz;
-
- // 构造方法注入
- public FaqTypeController(FaqTypeBiz faqTypeBiz) {
- this.faqTypeBiz = faqTypeBiz;
- }
-
- @Operation(summary = "保存Faq类型")
- @PostMapping("/save")
- public Result save(@RequestBody FaqTypeVO faqTypeVO){
- faqTypeBiz.saveFaqType(faqTypeVO);
- return Result.success();
- }
-
- }
- @Schema(description = "faq类型分页查询参数")
- @Data
- public class FaqTypePageReqVO extends BasePageVO {
-
- /**
- * 编号
- */
- @Schema(description = "编号")
- private String code;
-
- /**
- * 标题
- */
- @Schema(description = "标题")
- private String title;
-
- /**
- * 内容
- */
- @Schema(description = "内容")
- private String content;
-
- /**
- * 主管部门
- */
- @Schema(description = "主管部门")
- private String department;
-
-
- }
然后就可以启动了,如果启动报错,这个错误我在swagger里也写过:
- spring:
- mvc:
- pathmatch:
- matching-strategy: ant_path_matcher
文章传送:https://lsqingfeng.blog.csdn.net/article/details/123689652?spm=1001.2014.3001.5502
然后启动项目: 输入: ip:port/doc.html 就可以打开接口文档了,长的比swagger强。
这里在说一下,如果项目中添加了拦截器,就会导致接口文档出不来,就需要放开才行,我之前也讲过这个问题。
传送: https://lsqingfeng.blog.csdn.net/article/details/123678701?spm=1001.2014.3001.5502
在文章的第四部分,就是拦截器和跨域冲突。
我按照文章里的方式试了一下,发现还是不行。 所以如果遇到这个问题的同学,请使用如下最新的解决方式:还是添加配置类
- @Configuration
- @Slf4j
- public class SecurityConfiger implements WebMvcConfigurer {
-
- @Autowired
- private VerifyInterceptor verifyInterceptor;
-
- @Override
- public void addInterceptors(InterceptorRegistry registry) {
- InterceptorRegistration registration = registry.addInterceptor(verifyInterceptor);
- registration
- .addPathPatterns("/**")
- .excludePathPatterns("/oauth/callback")
- .excludePathPatterns("/doc.html/**")
- .excludePathPatterns("/swagger-resources/**")
- .excludePathPatterns("/error")
- .excludePathPatterns("/webjars/**")
- .excludePathPatterns("/doc.html")
- .excludePathPatterns("/api")
- .excludePathPatterns("/api-docs")
- .excludePathPatterns("/api-docs/**")
- .excludePathPatterns("/doc.html/**")
- .excludePathPatterns("/v2/**")
- .excludePathPatterns("/v3/**")
- .excludePathPatterns("/actuator/**");
- }
-
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry.addResourceHandler("/**")
- .addResourceLocations("classpath:/static/");
- registry.addResourceHandler("swagger-ui.html")
- .addResourceLocations("classpath:/META-INF/resources/");
- registry.addResourceHandler("/webjars/**")
- .addResourceLocations("classpath:/META-INF/resources/webjars/");
- registry.addResourceHandler("/doc.html")
- .addResourceLocations("classpath:/META-INF/resources/");
- }
-
- }
-
这个写的比之前的代码多一下,排除支持的也更多一些。
好了,再会!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。