Support multiple pathmapping in Swagger UI/Spring boot
Asked Answered
G

1

6

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.

Gulley answered 23/11, 2018 at 12:7 Comment(0)
G
14

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)).+\\/.*"));
    }
Gulley answered 22/12, 2018 at 12:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.