全网找了好久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;
}