knife4J 配置(生成接口文档)

发布时间 2023-03-22 21:13:16作者: 云淡风轻博客

1、概述

knife4j 是springfox-swagger的增强UI实现,为Java来发者在使用swagger的时候,能拥有一份简洁、强大的接口文档体验。

2、使用

1、导入依赖(pom.xml)


        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>

2、添加config配置


@Configuration
@EnableOpenApi
public class Knife4jConfig {

    private final OpenApiExtensionResolver openApiExtensionResolver;

    @Autowired
    public Knife4jConfig(OpenApiExtensionResolver openApiExtensionResolver) {
        this.openApiExtensionResolver = openApiExtensionResolver;
    }

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .groupName("1.0版本")
                .select()
                .paths(PathSelectors.any())
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .build()
                .extensions(openApiExtensionResolver.buildSettingExtensions());
    }
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("四六级后台管理接口文档")
                .description("四六级后台管理相关接口文档")
                .version("1.0.0")
                .termsOfServiceUrl("http://localhost:8083")
                .contact(new Contact("","",""))
                .build();
    }
}

3、修改配置文件(properties.yml)


knife4j:
  # 是否开启加强模式 true开启  false关闭
  enable: true
  setting:
    # 是否开启调试功能  true开启  false关闭
    enableDebug: true
  basic:
    # 是否开启认证功能  true开启  false关闭
    enable: false
    username: test
    password: 123456