SpringBoot3.x与SpringDoc OpenApi之Swagger接口排序
直接使用Swagger之后,发现所有的Controller 接口菜单都是无序的
先看一下效果
就是利用了一下 SpringDoc 提供的接口做了一下自定义排序
1.在Controller上加上注解
@Tag(name = "MenuController", description = "1 - 菜单管理")
这里需要注意 description 属性,在下面的代码里面有用到
2.实现自定义排序的接口
1 @Slf4j 2 @Component 3 public class OpenApiLocaleSort implements OpenApiLocaleCustomizer { 4 5 @Override 6 public void customise(OpenAPI openApi, Locale locale) { 7 List<Tag> tags = openApi.getTags(); 8 if (CollectionUtil.isNotEmpty(tags)) { 9 tags.sort(new Comparator<Tag>() { 10 @Override 11 public int compare(Tag o1, Tag o2) { 12 if (null == o1 || null == o2) { 13 return 0; 14 } 15 String d1 = o1.getDescription(); 16 if (StringUtils.isEmpty(d1) || !d1.contains("-")) { 17 return -1; 18 } 19 String d2 = o2.getDescription(); 20 if (StringUtils.isEmpty(d2) || !d2.contains("-")) { 21 return -1; 22 } 23 String[] s1 = d1.split("-"); 24 String[] s2 = d2.split("-"); 25 try { 26 return Integer.parseInt(s1[0].trim()) > Integer.parseInt(s2[0].trim()) ? 1 : -1; 27 } catch (Exception e) { 28 log.error(e.getMessage(), e); 29 } 30 return 0; 31 } 32 }); 33 } 34 } 35 }
这里就是利用 description 属性来进行排序。
作者:Se7end
声明:本博客文章为原创,只代表本人在工作学习中某一时间内总结的观点或结论。转载时请在文章页面明显位置给出原文链接。