Swagger2 进行分组_与梦想同在的博客-CSDN博客_swagger 分组

Swagger是一个很好的api文档,如果我们的接口过于多,那么一个页面很难展示,查找不方便,那么我们就对swagger进行分组。

分组策略为按包名称分组,另一个是按请求路径进行分类。

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {


//设置显示状态,value是在pom中配置的
@Value("${meinergy.config.swagger:false}")
    private boolean swaggerShow;

    @Bean
    public Docket createRestApiForAll() {
        return new Docket(DocumentationType.SWAGGER_2).enable(swaggerShow).apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.tyut.controller"))
                .paths(PathSelectors.any()).build().groupName("所有api").pathMapping("/");
    }

    //根据包进行分组

    @Bean
    public Docket createRestApiForAuth() {
        return new Docket(DocumentationType.SWAGGER_2).enable(swaggerShow).apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.tyut.controller.auth"))
                .paths(PathSelectors.any()).build().groupName("用户管理").pathMapping("/");
    }

    @Bean
    public Docket createRestApiForCs() {
        return new Docket(DocumentationType.SWAGGER_2).enable(swaggerShow).apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.tyut.controller.cs"))
                .paths(PathSelectors.any()).build().groupName("客户满意度").pathMapping("/");
    }
    
    // 按照路径进行分组
    @Bean
    public Docket web_api_admin() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo("admin-api", "系统管理员", "1.0"))
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/api/admin/**"))
                .build()
                .groupName("系统管理员")
                .pathMapping("/");
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("[项目名称]").description("[项目描述]").version("1.0.0").build();
    }
}

原网址: 访问
创建于: 2021-11-23 16:57:53
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论