首先是依赖导入
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
然后是配置文件中的配置
/**
* 配置类,注册web层相关组件
*/
@Configuration
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
/**
* 通过knife4j生成接口文档
* @return
*/
@Bean
public Docket docket() {
ApiInfo apiInfo = new ApiInfoBuilder()
.title("接口文档标题")
.version("2.0")//版本号
.description("接口文档详细信息")
.build();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo)
.select()
.apis(RequestHandlerSelectors.basePackage("填写要管理的包路径"))//一般管理controller包下的接口类
.paths(PathSelectors.any())
.build();
return docket;
}
/**
* 设置静态资源映射
* @param registry
*/
protected void addResourceHandlers(ResourceHandlerRegistry registry) {//addResourceHandlers为WebMvcConfigurationSupport类中的实现方法
registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");//之后通过,访问地址/doc.html来访问端口测试页面
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
标签:knife4j,docket,apiInfo,文档,registry,Api,Swagger,Docket From: https://www.cnblogs.com/cyknote/p/17705895.html