API 管理工具
一、Swagger
@Bean
//Swagger实例Bean是Docket
public Docket docket(Environment environment){
Profiles profiles = Profiles.of("","","");
//Profiles.of用于什么环境下
boolean flag = environment.acceptsProfiles(profiles);
//acceptsProfiles监听对象
List<RequestParameter> pars = new ArrayList<>();
RequestParameterBuilder amTokenPar = new RequestParameterBuilder();
amTokenPar.name("")
.description("")
.in(ParameterType.HEADER)
.required(false)
.build();
RequestParameterBuilder TokenPar = new RequestParameterBuilder();
TokenPar.name("")
.description("")
.in(ParameterType.HEADER)
.required(false)
.build();
pars.add(amTokenPar.build());
pars.add(TokenPar .build());
RequestParameter requestParameter = new RequestParameter();
List<RequestParameter> parameters = new ArrayList<>();
return new Docket(DocumentationType.SWAGGER_2)
.globalRequestParameters(pars)
.apiInfo(apiInfo())//关联上apiInfo
.groupName("")//分组
.enable(flag)//启用或者不启用
.select()//通过select()方法配置扫描接口
.apis(RequestHandlerSelectors.basePackage(""))//配置扫描哪个包
.build();
private ApiInfo apiInfo(){
//apiInfo()属性配置文档信息
Contact mycontact = new Contact("","");
//package springfox.document.service
return new ApiInfoBuilder()
.title("Api Document")//标题
.description("Api Description")
.contact(myContact)//描述
.license("Apache 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")//许可证
.version("1.0")
.extensions(new ArrayList<>())//扩展
.build();
}
}
//生产环境下应该关闭,会占用内存
//导入依赖
<dependency>
<groupId>com.github.xiaomin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
二、Knife4j
Knife4j是一个集Swagger2和OpenAPI3为一体的增强解决方案。
//导入依赖
<dependency>
<groupId>com.github.xiaomin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
//properties配置
Knife4j:
#开启增强配置
enable:true
#开启生产环境屏蔽
production:true
server:
port:8808
spring:
mvc:
pathmatch:
#Springfox使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher,因此需要配置此参数
matching-strategy:ant_path_matcher