Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize endpoints of dockets with springfox Swagger

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

like image 425
salidou Avatar asked May 31 '17 12:05

salidou


People also ask

How does Springfox work?

Springfox works by examining an application, once, at runtime to infer API semantics based on spring configurations, class structure and various compile time java Annotations.

What is docket in Swagger configuration?

The configuration of Swagger mainly centres around the Docket bean. A docket is an object that contains all the customizable properties you set and is used by Swagger to generate the documentation. After Docket is defined, then used select() method to get a docket builder object from class ApiSelectorBuilder.

Does Springfox support OpenAPI 3?

Recently, the popular Springfox project released the long-awaited v3 of their library with support for OpenAPI 3 and Spring 5 (only annotation-based API is supported). It also comes with a ready-to-use Spring Boot starter which replaces a host of dependencies that were required in earlier versions.


1 Answers

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");


}
}
like image 131
salidou Avatar answered Oct 11 '22 00:10

salidou