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