自动生成API文档,https://gitee.com/xiaoym/knife4j 这个还是挺好用的,需求希望在开发环境下启用API文档,而生产环境下则关闭API文档,我们可以在Knife4j的配置代码中,加入@ConditionalOnProperty来根据当前的环境判断是否使用Knife4j。
Knife4jConfig 生产环境禁用 @ConditionalOnProperty(name = "my.product-env", havingValue = "false")
意思是查看application.properties或者application.yml中,my.product-env如果是false的(不是生产环境)才开启Knife4j,如果你是原生Swagger,也是同样配置的地方,加上这个Annotation就可以了。
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2WebFlux; import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc; /** * 2021/11/08 08:04 * @since: knife4j-spring-boot2-demo 1.0 * see https://gitee.com/xiaoym/knife4j */ @EnableKnife4j @EnableSwagger2WebMvc @EnableSwagger2WebFlux @Configuration @ConditionalOnProperty(name = "my.product-env", havingValue = "false") public class Knife4jConfig { @Bean(value = "defaultApi2") public Docket defaultApi2() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) //分组名称 // .groupName("测试分组") .select() //这里指定Controller扫描包路径 .apis(RequestHandlerSelectors.basePackage("com.terrynow.test.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Info APIs") .description("Info APIs") // .termsOfServiceUrl("http://www.group.com/") .contact("[email protected]") .version("1.0") .build(); } }
文章评论