【问题标题】:Support multiple pathmapping in Swagger UI/Spring boot支持 Swagger UI/Spring boot 中的多路径映射
【发布时间】:2019-04-26 01:14:48
【问题描述】:

我在 Spring boot(版本 1.5.9.RELEASE)项目中使用 swagger 2.0。 Swagger 工作正常,但现在文档有数百个 api,我想在不同的不同 url 上重定向文档。我有像 blow 这样的 swagger 配置。

@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();
    }
}

请提出任何建议。提前致谢。

【问题讨论】:

    标签: spring spring-boot swagger swagger-2.0


    【解决方案1】:

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

        @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)).+\\/.*"));
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-20
      • 1970-01-01
      • 2017-11-13
      • 2017-01-28
      • 2020-09-12
      • 2019-08-06
      • 2022-01-21
      • 2021-02-04
      相关资源
      最近更新 更多