【问题标题】:No operations defined in spec! while specifying multiple paths in swagger ui规范中没有定义操作!在 swagger ui 中指定多个路径时
【发布时间】:2018-05-11 11:26:28
【问题描述】:

我想在 Swagger ui 中显示两个 REST API 端点:/cart 和 /post。

当我指定 /cart 或 /post 工作正常但都显示错误为

规范中没有定义操作!

在 swagger-ui 中

@Bean
public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
        .select()
        .apis(RequestHandlerSelectors.any())
        .paths(PathSelectors.ant("/cart"))
        .paths(PathSelectors.ant("/post"))
        .build();
}

【问题讨论】:

  • 这是 Springfox 还是其他框架?
  • 不,是 springfox
  • return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage(("com.valens.compony"))) .paths(Predicates.or( PathSelectors.regex(" /cart"),PathSelectors.regex("/post"))) .build() .pathMapping("/") 我找到了这个解决方案,但它只适用于java而不是scala
  • 验证端点根路径。

标签: java scala swagger swagger-ui


【解决方案1】:

另一种选择是使用 .paths(PathSelectors.any()) 而不是 .paths(PathSelectors.ant("/cart" )).paths(PathSelectors.ant("/post"))

【讨论】:

    【解决方案2】:

    使用 Spring boot 2.6.x 你还需要:

    spring:  
      mvc:
        pathmatch:
          matching-strategy: ant_path_matcher
    

    【讨论】:

    • 这个技巧解决了我的问题。你能详细说明你是如何解决这个问题的吗@Peyman?如果你能把这个原始文档/问题点分享出来,我会很棒。
    【解决方案3】:

    因为你使用AND条件

      public ApiSelectorBuilder paths(Predicate<String> selector) {
        pathSelector = pathSelector.and(selector);
        return this;
      }
    

    您可以使用OR 子句组合条件

    .paths(PathSelectors.ant("/cart").or(PathSelectors.ant("/post")))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      • 2020-10-01
      • 2020-11-27
      • 2023-03-08
      • 2019-02-10
      相关资源
      最近更新 更多