knife4j是一个建立在open-doc2.0和swagger-ui文档基础之上的由国人开发的文档。
其官方网址是https://doc.xiaominfo.com/docs/action/springboot。
使用流程:
1.首先导入依赖包
下面展示一些 内联代码片
。
<!-- 基于Swegger-bootstrap-ui的knife4j自动生成文档 的依赖-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
<version>4.0.0</version>
</dependency>
2.编写配置类
package com.example.demo.Config;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
@Configuration
@EnableSwagger2WebMvc
class Knife4jConfiguration {
@Bean(value = "dockerBean")
public Docket dockerBean() {
//指定使用Swagger2规范
Docket docket=new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
//描述字段支持Markdown语法
.description("文档描述")
.termsOfServiceUrl("https://doc.xiaominfo.com/")
.contact("作者")
.version("版本号")
.build())
//分组名称
.groupName("分组名称")
.select()
//这里指定Controller扫描包路径
//下面这一步至关重要,如果配置不对则不会扫描Controller下的接口
.apis(RequestHandlerSelectors.basePackage("com.example.demo.Controller"))
.paths(PathSelectors.any())
.build();
return docket;
}
}
3.编写全局配置
knife4j:
enable: true
setting:
language: zh_cn
第一个属性为是否开启ui增强功能
第二个为设置,包括文档语言等。
4.生成结果