knife4j是基于swagger二次开发的框架,提供了丰富的页面管理,增强了原有的wagger功能
一, 添加依赖
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
二, 编写配置文件
package com.shuinfo.ods_poc.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
@EnableOpenApi
public class SwaggerConfig {
@Autowired
private Environment environment;
@Bean
public Docket docket() {
// 设置显示的swagger环境信息
Profiles profiles = Profiles.of("dev", "local");
// 判断是否处在自己设定的环境当中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("ods_poc") // 配置api文档的分组
.enable(flag) // 配置是否开启swagger
.select()
.apis(RequestHandlerSelectors.basePackage("com.shuinfo.ods_poc")) //配置扫描路径
.paths(PathSelectors.any()) // 配置过滤哪些
.build();
}
// api基本信息
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("starbuck-ods-webflux-poc")
.description("")
.termsOfServiceUrl("http://www.xxxxxx.com/")
.contact(new Contact("xxxxxx", "https://www.xxxxxxx.com", ""))
.version("0.0.1")
.build();
}
}