【问题标题】:Customize endpoints of dockets with springfox Swagger使用 springfox Swagger 自定义文档的端点
【发布时间】:2017-11-01 07:00:47
【问题描述】:

我在互联网上搜索了如何自定义我的多个案卷的端点,但没有找到答案。

我的模块有几个 API。我想在不同的端点上生成 Swagger 文档,每个端点都位于其相应 API 的根目录上。例如:

  • localhost:8080/v1/subscriptions/doc

  • localhost:8080/v1/buckets/doc

我发现只有一种方法可以为我的案卷设置不同的端点,但 URL 与我想要的不对应。他们是:

  • localhost:8080/doc?group=subscriptions

  • localhost:8080/doc?group=buckets

这是我的 Swagger 配置文件

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

在我的 application.yml 文件中,我写了:

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

您知道按照我想要的方式定义端点的方法吗?

提前致谢

【问题讨论】:

    标签: rest swagger springfox


    【解决方案1】:

    我找到了答案!

    @Configuration
    @EnableWebMvc
    public class WebConfig extends WebMvcConfigurerAdapter {
    
    
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
    
        registry.addRedirectViewController("/v1/subscriptions/doc", "/doc?group=subscriptions");
    
    
    }
    }
    

    【讨论】:

    • 对我不起作用。 “/doc?group=subscriptions”路径未知。
    • @DirkSchumacher doc 部分网址来自springfox.documentation.swagger.v2.path: "/doc"group=xxx 来自 .groupName("xxx")。您的配置中有正确的值吗?
    猜你喜欢
    • 2019-07-27
    • 2020-04-25
    • 2016-05-01
    • 2020-11-27
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    相关资源
    最近更新 更多