ruoyi-cloud-plus集成knife4j接口排序问题

本文介绍了如何在SpringDocAutoConfiguration中修改openApiCustomiser方法,实现Controller类中接口方法按照接口描述进行排序,通过比较每个接口的第一个操作的描述来决定顺序。
摘要由CSDN通过智能技术生成

全网找了好久Controller类中的方法(以下称为接口)排序,各种方法试了都不行
个人解决方案如下:

修改SpringDocAutoConfiguration中openApiCustomiser方法的返回值,根据接口名称重新排序

PlusPaths newPaths2 = newPaths.entrySet()
    .stream()
    .sorted(Map.Entry.<String, PathItem>comparingByValue((e1, e2) -> {
        String desc1 = "";
        Operation operation1 = getFirstOperation(e1);
        if (operation1 != null) {
            if (StrUtil.isNotBlank(operation1.getDescription())) {
                desc1 = operation1.getDescription();
            }
        }

        String desc2 = "";
        Operation operation2 = getFirstOperation(e2);
        if (operation2 != null) {
            if (StrUtil.isNotBlank(operation2.getDescription())) {
                desc2 = operation2.getDescription();
            }
        }

        return desc1.compareTo(desc2);
    }))
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, PlusPaths::new));
private Operation getFirstOperation(PathItem pathItem) {
    if (pathItem.getGet() != null) {
        return pathItem.getGet();
    } else if (pathItem.getPut() != null) {
        return pathItem.getPut();
    } else if (pathItem.getPost() != null) {
        return pathItem.getPost();
    } else if (pathItem.getDelete() != null) {
        return pathItem.getDelete();
    } else if (pathItem.getOptions() != null) {
        return pathItem.getOptions();
    } else if (pathItem.getHead() != null) {
        return pathItem.getHead();
    } else if (pathItem.getPatch() != null) {
        return pathItem.getPatch();
    } else if (pathItem.getTrace() != null) {
        return pathItem.getTrace();
    }
    return null;
}
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值