Customize endpoints of dockets with springfox Swagger

salidou picture salidou · May 31, 2017 · Viewed 7.2k times · Source

I've searched on the internet how to customize endpoints of my multiple dockets, but haven't found the answer.

My module has several APIs. I want to generate Swagger documentation on different endpoints, each one positioned on the root of its corresponding API. For example :

  • localhost:8080/v1/subscriptions/doc

  • localhost:8080/v1/buckets/doc

I've found only one way to have different endpoints for my dockets, but the URL don't correspond to what I want. They are :

  • localhost:8080/doc?group=subscriptions

  • localhost:8080/doc?group=buckets

Here is my Swagger configuration file

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

@Value("${info.version}")
private String version;

@Bean
public Docket subscriptionsApi() {
    return new Docket(DocumentationType.SWAGGER_2)
            .groupName("subscriptions")
            .apiInfo(subscriptionsApiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.mymodule"))
            .paths(PathSelectors.ant("/v1/subscriptions/**"))
            .build();
}

@Bean
public Docket bucketsApi() {
    return new Docket(DocumentationType.SWAGGER_2)
            .groupName("buckets")
            .apiInfo(bucketsApiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.mymodule"))
            .paths(PathSelectors.ant("/v1/buckets/**"))
            .build();
}

private ApiInfo subscriptionsApiInfo() {
    return new ApiInfoBuilder()
            .title("Subscriptions Api definition")
            .description("Subscriptions Api definition")
            .version(version)
            .build();
}

private ApiInfo bucketsApiInfo() {
    return new ApiInfoBuilder()
            .title("Bucket Api definition")
            .description("Bucket Api definition")
            .version(version)
            .build();
}
}

And in my application.yml file, I've written :

springfox.documentation.swagger.v2.path: "/doc"

Do you know a way to define the endpoints on the way I want?

Thanks in advance

Answer

salidou picture salidou · May 31, 2017

I've found the answer!

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {


@Override
public void addViewControllers(ViewControllerRegistry registry) {

    registry.addRedirectViewController("/v1/subscriptions/doc", "/doc?group=subscriptions");


}
}