前言
项目api编写有文档但是调试麻烦,需要借助postman/apipost等第三方工具,需要在开发框架中内置接口管理及调试功能增加开发便捷性
pom配置
说明,框架使用springboot版本为2.0.0.RELEASE
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.3</version>
</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");
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()
.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代码配置的方式控制生产环境关闭文档资源的访问
|