当前位置: 首页 > web >正文

Spring boot 集成 Knife4j

knife4j官网:https://doc.xiaominfo.com/docs/quick-start

1. 引入Knife4j相关依赖

<!-- knife4j-->
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>2.0.8</version>
</dependency>
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.10.5</version>
</dependency><!-- knife4j -->
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-aggregation-spring-boot-starter</artifactId><version>2.0.8</version>
</dependency><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-ui</artifactId><version>2.0.8</version>
</dependency>

2. 创建Knife4J配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;@Configuration
@EnableSwagger2WebMvc
public class Swagger2Config {@Beanpublic Docket defaultApi() {return new Docket(DocumentationType.SWAGGER_2).groupName("系统后台接口文档").apiInfo(defaultApiInfo()).select().apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)).paths(PathSelectors.any()).build();}private ApiInfo defaultApiInfo() {return new ApiInfoBuilder().title("系统后台接口文档").description("系统后台接口文档").contact(new Contact("开发组", "", "")).version("1.0").build();}}

相关配置

程序main方法添加注解

@EnableSwagger2WebMvc

控制器添加注解

@Api(tags="日志管理") // 控制器
@ApiOperation("所有日志") // 方法

常见问题

TypeError: n.forEach is not a function

解决方案:

  1. 确认请求是否有拦截器,由于拦截器会把所有请求都拦截下来,而swagger(knife4j)的接口页面也会被拦截,所以需要进行排除拦截设置。

优化配置如下:

 public void addInterceptors(InterceptorRegistry registry) {// 对swagger的请求不进行拦截String[] excludePatterns = new String[]{"/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**","/api", "/api-docs", "/api-docs/**", "/doc.html/**"};//添加token拦截器InterceptorRegistration tokenFilter = registry.addInterceptor(new TokenInterceptor());tokenFilter.addPathPatterns("/**").excludePathPatterns(excludePatterns);//不拦截的路径,如获取系统时间等公共服务tokenFilter.excludePathPatterns("/common");}

但该方案并未生效,做如下修改,在拦截方法preHandle中获取request请求详细信息,对请求路径做处理

// swagger
if (request.getRequestURL().toString().contains("swagger")) return true;

SpringBoot使用拦截器和swagger(knife4j)配置_拦截器放行knif4-CSDN博客

  1. 另一种解决方案,暂未遇到。

配置knife4j时出现问题 TypeError: n.forEach is not a function_springfox.documentation.spring.web.onservletbasedw-CSDN博客

参考文章

SpringBoot整合knife4j(快速入门超详细版)-CSDN博客
springboot集成swagger之knife4j实战(升级版) - 小小程序猿-DB - 博客园

http://www.xdnf.cn/news/7484.html

相关文章:

  • 网络I/O学习-poll(三)
  • 范围管理的实践策略与创新应用
  • 头歌之软件工程-数据设计
  • 433. 最小基因变化
  • AcWing 223. 阿九大战朱最学——扩展欧几里得算法
  • Javascript本地存储的方式有哪些?区别及应用场景?(含Deep Seek讲解)
  • [长城杯 2024]anote
  • 怎么利用JS根据坐标判断构成单个多边形是否合法
  • HarmonyOS Next应用分层架构下组件封装开发实践
  • 子网前缀长度
  • 【General Agent Benchmark】论文分享No.12:LLF-Bench
  • Python训练第三十天
  • 新一代请求库niquests使用入门
  • 告别Spring AI!我的Java轻量AI框架实践(支持多模型接入|注解式MCP架构|附开源地址)
  • “星睿O6”AI PC 开发套件评测: NPU 算力测评(1)
  • DAY30
  • Docker 运维管理
  • 使用shell快速删除Docker容器、镜像和存储内容
  • Python海龟绘图-斗地主
  • redis在spring boot中异常退出
  • 【C语言】贪吃蛇小游戏
  • Python 实例传递的艺术:四大方法解析与最佳实践
  • 每日算法 -【Swift 算法】不含重复字符的最长子串:暴力解法 vs 滑动窗口
  • Python 实现图片浏览和选择工具
  • 出海跨境电商内容管理难?Baklib 助力打造多语言知识库与智能素材中心
  • Stable Diffusion 学习笔记02
  • 【Nextcloud】使用 LNMP 架构搭建私有云存储:Nextcloud 实战指南
  • TYUT-企业级开发教程-第5章
  • 【C++]string模拟实现
  • laravel 通过Validator::make验证后,如何拿到验证后的值