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 {

    @Bean
    public 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 - 博客园

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值