【问题标题】:How do I write a custom sorter to sort my springdoc swagger tags by name in the UI?如何编写自定义排序器以在 UI 中按名称对 springdoc swagger 标签进行排序?
【发布时间】:2020-07-03 17:00:53
【问题描述】:

我正在使用最新版本 (1.3.0) 的 springdoc-openapi。现在我想按“名称”属性在 UI 中对标签进行排序

我知道“springdoc.swagger-ui.tagsSorter”配置,并且我可以使用自定义排序器功能。但我找不到函数应该是什么样子的例子。

我尝试了以下似乎不起作用的方法:

springdoc.swagger-ui.tagsSorter=(a, b) => a.get("name").localeCompare(b.get("name"))

【问题讨论】:

    标签: swagger swagger-ui springdoc


    【解决方案1】:

    默认情况下,您可以按字母顺序对标签进行排序:

    您可以控制标签顺序,使用 OpenApiCustomiser 并定义您自己的比较器:

    @Bean
    public OpenApiCustomiser sortTagsAlphabetically() {
        return openApi -> openApi.setTags(openApi.getTags()
                .stream()
                .sorted(Comparator.comparing(tag -> StringUtils.stripAccents(tag.getName())))
                .collect(Collectors.toList()));
    }
    

    【讨论】:

    • 既然Springdoc对标签、字段值按字母顺序排序,我们如何禁用这个呢?
    • 按预期工作,谢谢+1
    【解决方案2】:

    参考@brianbro's answer,如https://springdoc.org/faq.html#how-can-i-sort-endpoints-alphabetically建议的那样

    我加了

    @Tag(name="1. Admin endpoints")

    @Tag(name = "2. Everyone's enpoints!")

    及以下对 application.yml 的支持:

    springdoc.swagger-ui.tagsSorter=alpha

    并且可以看到它们根据我的招摇 UI 上的编号进行排序。

    【讨论】:

      猜你喜欢
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      • 1970-01-01
      • 2012-02-13
      • 2014-09-17
      相关资源
      最近更新 更多