Swagger:swagger分组配置和文档介绍
花开堪折直需折,莫待无花空折枝
分组配置:
没有使用分组配置时,所用的api都在默认的分组下,如果api接口过多,显然是不利于管理的:

配置api文档分组:创建swagger2配置文件:Swagger2Config
@Configuration
@EnableSwagger2
public class Swagger2Config {
//前台文档分组
@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
//分组名称
.groupName("webApi")
.select()
//只显示api路径下的页面
.paths(Predicates.and(PathSelectors.regex("/api/.*")))
.build();
}
//后台文档分组
@Bean
public Docket adminApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
//分组名称
.groupName("adminApi")
.select()
//只显示admin路径下的页面
.paths(Predicates.and(PathSelectors.regex("/admin/.*")))
.build();
}
}
简单说明:“ .paths(Predicates.and(PathSelectors.regex("/api/.*")))"中“and”可以换为”not“,表示禁止的路径
效果图:

分组+文档介绍配置:
只需要在上面的基础上进行一些修改即可,代码:
@Configuration
@EnableSwagger2
public class Swagger2Config {
//前台文档分组
@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
//分组名称
.groupName("webApi")
//添加分档信息配置,通过webApiInfo()函数返回一个ApiInfo对象
.apiInfo(webApiInfo())
.select()
//只显示admin路径下的页面
.paths(Predicates.and(PathSelectors.regex("/api/.*")))
.build();
}
//后台文档分组
@Bean
public Docket adminApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
//分组名称
.groupName("adminApi")
//添加分档信息配置,通过adminApiInfo()函数返回一个ApiInfo对象
.apiInfo(adminApiInfo())
.select()
//只显示admin路径下的页面
.paths(Predicates.and(PathSelectors.regex("/admin/.*")))
.build();
}
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
.title("网站Api文档")
.description("描述了网站微服务接口定义")
.version("1.0")
.contact(new Contact("LD","","1559439467@qq.com"))
.build();
}
private ApiInfo adminApiInfo(){
return new ApiInfoBuilder()
.title("后台Api文档")
.description("描述了后台管理系统微服务接口定义")
.version("1.0")
.contact(new Contact("LD","","1559439467@qq.com"))
.build();
}
}
添加了一个“apiInfo()”,该函数需要接收一个ApiInfo对象(在上面的代码中通过函数返回ApiInfo对象),包含了文档信息的各种属性:

效果:

转载请注明来源,谢谢
本文档介绍了如何在Swagger中进行分组配置,以更好地管理API接口。通过创建Swagger2Config配置文件,利用Predicates对API路径进行筛选,实现接口的分组。同时,展示了如何添加文档介绍,包括使用ApiInfo对象填充文档的各种属性,以提供更详细的接口信息。

被折叠的 条评论
为什么被折叠?



