其他分享
首页 > 其他分享> > SpringBoot集成knife4j

SpringBoot集成knife4j

作者:互联网

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.3</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.5.14</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

 在项目中一般会存在鉴权拦截器,需要将knife4j相关资源放行

anonymousAccessList.add("/swagger-resources");
anonymousAccessList.add("/swagger-resources/configuration/ui");
anonymousAccessList.add("/v2/api-docs");
anonymousAccessList.add("/v2/api-docs-ext");
anonymousAccessList.add("/doc.html");
anonymousAccessList.add("/webjars/*");
anonymousAccessList.add("/favicon.ico");

 其中application.properties中spring.profiles.active=pro

 Config配置

@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {
    @Value("${spring.profiles.active}")
    private String active;

    @Bean(value = "frameAPI")
    @Conditional({SwaggerProjectCondition.class})
    public Docket frameAPI() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo("xxx", "xxx"))
                //分组名称
                .groupName("xxx")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.xxx.xxx"))
                .paths(PathSelectors.any())
                .build();
        docket.enable(!active.equals("pro"));
        return docket;
    }

    private ApiInfo apiInfo(String appTag, String docName) {
        return new ApiInfoBuilder()
                .title(appTag + " api")
                .description(docName)
                .contact("admin")
                .version("1.0")
                .build();
    }

}

 注:因为使用knife4j版本不是最新,所以通过docket.enable代码配置的方式控制生产环境关闭文档资源的访问

package com.ruoyi.web.core.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.ruoyi.common.config.RuoYiConfig;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
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.EnableSwagger2;

/**
 * Swagger2的接口配置
 * 
 * @author ruoyi
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig
{
    /** 是否开启swagger */
    @Value("${swagger.enabled}")
    private boolean enabled;
    
    /**
     * 创建API
     */
    @Bean
    public Docket createRestApi()
    {
        return new Docket(DocumentationType.SWAGGER_2)
                // 是否启用Swagger
                .enable(enabled)
                // 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息)
                .apiInfo(apiInfo())
                // 设置哪些接口暴露给Swagger展示
                .select()
                // 扫描所有有注解的api,用这种方式更灵活
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 扫描指定包中的swagger注解
                //.apis(RequestHandlerSelectors.basePackage("com.ruoyi.project.tool.swagger"))
                // 扫描所有 .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 添加摘要信息
     */
    private ApiInfo apiInfo()
    {
        // 用ApiInfoBuilder进行定制
        return new ApiInfoBuilder()
                // 设置标题
                .title("标题:若依管理系统_接口文档")
                // 描述
                .description("描述:用于管理集团旗下公司的人员信息,具体包括XXX,XXX模块...")
                // 作者信息
                .contact(new Contact(RuoYiConfig.getName(), null, null))
                // 版本
                .version("版本号:" + RuoYiConfig.getVersion())
                .build();
    }
}

 

标签:集成,knife4j,swagger,SpringBoot,anonymousAccessList,documentation,add,import,sprin
来源: https://www.cnblogs.com/JYB2021/p/16629501.html