Swagger API操作排序 [英] Swagger API Operations Ordering
问题描述
如何按字母顺序对操作进行排序,例如删除,获取, POST ,放置.
How do I sort my operation by method alphabetically e.g. DELETE, GET, POST, PUT.
我已经阅读了这篇文章,但是它是HTML格式的,但就我而言,我将Swagger集成到了Spring Boot中,因此在创建Docket时需要对其进行排序.
I have read from this post but it is in HTML but in my case, I have integrated Swagger into Spring Boot so I need to sort it when creating a Docket.
然后我在Docket中注意到了此方法 operationOrdering()
,但是我仍然无法使其正常工作.
Then I noticed this method operationOrdering()
in Docket, but I still cannot make it work.
推荐答案
我正在使用Springfox 2.8.0版,以下代码段适用于我记录的API:
I am using Springfox version 2.8.0 and following code snippet works for my documented API:
@Bean
UiConfiguration uiConfig() {
return UiConfigurationBuilder
.builder()
.operationsSorter(OperationsSorter.METHOD)
...
.build();
}
有2个可能的值:
-
OperationsSorter.ALPHA
-按路径 的字母顺序对API端点进行排序 -
OperationsSorter.METHOD
-通过方法 按字母顺序对API端点进行排序
OperationsSorter.ALPHA
- sorts API endpoints alphabetically by pathOperationsSorter.METHOD
- sorts API endpoints alphabetically by method
OperationsSorter.METHOD
.
替代:
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
...
.operationOrdering(new Ordering<Operation>() {
@Override
public int compare(Operation left, Operation right) {
return left.getMethod().name().compareTo(right.getMethod().name());
}
})
}
但是,由于Springfox中的一个错误似乎仍处于活动状态,因此无法正常工作(操作订购无效).
However, this does not work because of a bug in Springfox which seems to be still active (Operation ordering is not working).
这篇关于Swagger API操作排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!