首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Swagger UI/Spring boot中支持多路径映射

在Swagger UI/Spring boot中支持多路径映射
EN

Stack Overflow用户
提问于 2018-11-23 20:07:20
回答 1查看 12.8K关注 0票数 6

我在Spring boot(版本1.5.9.RELEASE)项目中使用swagger 2.0。Swagger运行良好,但现在文档有数百个api,我想在不同的now上重定向文档。

代码语言:javascript
运行
复制
@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();
    }
}

请给我一些建议。提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-22 20:28:36

最后,我根据它们的url将这些api分成组,如下代码段所示,创建了三个组,一个用于设置,另一个用于产品,最后一个包含除设置和产品之外的所有其他文档。

代码语言:javascript
运行
复制
    @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)).+\\/.*"));
    }
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53446447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档