依赖
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.9</version>
</dependency>
yml
knife4j:
enable: true
配置类
@Configuration
//@EnableSwagger2
@EnableSwagger2WebMvc
public class Swagger2Config {
// 配置swagger2核心配置 docket
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2) // 指定api类型为swagger2
.apiInfo(apiInfo()) // 用于定义api文档汇总信息
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
// .apis(Predicates.or(userPredicate))
.paths(PathSelectors.any()) // 所有controller
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("api") // 文档页标题
.contact(new Contact("1",
"2",
"3")) // 联系人信息
.description("xxx提供的api文档") // 详细信息
.version("1.0.1") // 文档版本号
.termsOfServiceUrl("https://www.baidu.com") // 网站地址
.build();
}
}
springboot 启动类扫描
@ComponentScan(basePackages = {"xx.xxx"})