Customize endpoints of dockets with springfox Swagger

99封情书 提交于 2019-12-04 10:51:27

问题


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


回答1:


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


}
}


来源:https://stackoverflow.com/questions/44285341/customize-endpoints-of-dockets-with-springfox-swagger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!