在Swagger UI/Spring Boot中支持多个路径映射 [英] Support multiple pathmapping in Swagger UI/Spring boot

查看:1119
本文介绍了在Swagger UI/Spring Boot中支持多个路径映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring Boot(版本1.5.9.RELEASE)项目中使用了swagger 2.0. Swagger可以正常工作,但是现在文档中有数百个api,我想重定向文档到不同的URL.我具有诸如blow之类的swagger配置.

I am using swagger 2.0 in a Spring boot(version 1.5.9.RELEASE) project. Swagger works fine but now documentation have hundreds of api and I want to redirect documentation on different different urls.I am having swagger configuration like blow.

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket postsApi() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("public-api")
                .apiInfo(apiInfo()).select().paths(postPaths()).build();
    }

    private Predicate<String> postPaths() {
        return or(regex("/api/posts.*"), or(regex("/api/.*"), regex("/secure/api/.*")));
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("Swagger API")
                .description("Swagger Integration with Spring Boot")
                .termsOfServiceUrl(null)
                .license(null)
                .licenseUrl(null).version("1.0").build();
    }
}

请提出任何建议.预先感谢.

Please suggest any way. Thanks in advance.

推荐答案

最后,我将这些api根据其url分为几组,如下代码段,为设置创建三个组,为产品创建另一个,最后一个包含所有其他文档,但设置和产品除外.

Finally I break these api's into groups basis on their url as following code segment, creates three group one for Settings, another for Products and last one contains all the other documentation except settings and products.

    @Bean
    public Docket swaggerSettingsApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("Settings")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xyz"))
                .paths(regex("/secure/api/v1/settings/.*"))
                .build()
                .apiInfo(new ApiInfoBuilder().version("1.0").title("Settings API").build())
                .globalOperationParameters(operationParameters());
    }

    @Bean
    public Docket swaggerProductApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("Product")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xyz.modules.v1"))
                .paths(productPaths())
                .build()
                .apiInfo(new ApiInfoBuilder().version("1.0").title("Product API").build())
                .globalOperationParameters(operationParameters());
    }

    @Bean
    public Docket swaggerModuleApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("Others")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xyz.modules.v1"))
                .paths(postPaths())
                .build()
                .apiInfo(new ApiInfoBuilder().version("1.0").title("Other Modules API").build())
                .globalOperationParameters(operationParameters());
    }

      private Predicate<String> postPaths() {
        return or(regex("^(?=\\/secure\\/api\\/v1\\/)(?!.*(settings|products)).+\\/.*"));
    }

这篇关于在Swagger UI/Spring Boot中支持多个路径映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆