How do I write a custom sorter to sort my springdoc swagger tags by name in the UI?

孤街浪徒 提交于 2021-01-28 11:47:47

问题


I am using springdoc-openapi with the latest version (1.3.0). Now I would like sort my tags in the UI by "name" property.

I know about the "springdoc.swagger-ui.tagsSorter" configuration and that I can use a custom sorter function. But I cannot find examples how the function should look like.

I tried the following which does not seem to work:

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


回答1:


By default, you can sort tags alphabetically:

  • https://springdoc.org/faq.html#how-can-i-sort-endpoints-alphabetically

You can have control on the tags order, using OpenApiCustomiser and define your own Comparator:

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


来源:https://stackoverflow.com/questions/60810048/how-do-i-write-a-custom-sorter-to-sort-my-springdoc-swagger-tags-by-name-in-the

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