18

How do I sort my operation by method alphabetically e.g. DELETE, GET, POST, PUT.

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.

Sort API methods in Swagger UI

Then I noticed this method operationOrdering() in Docket, but I still cannot make it work.

Vladas Maier
  • 2,054
  • 2
  • 22
  • 34
Javatar
  • 623
  • 3
  • 8
  • 20

5 Answers5

9

With Spring Boot 2.4 and OpenAPI the following properties in application.properties may be of interest:

  • springdoc.swagger-ui.tagsSorter=alpha
  • springdoc.swagger-ui.operations-sorter=alpha
mp31415
  • 6,531
  • 1
  • 44
  • 34
  • 1
    I'm not sure if there's a difference between: `springdoc.swagger-ui.operationsSorter=alpha` & `springdoc.swagger-ui.operations-sorter=alpha` but the former is used on their docs:https://springdoc.org/faq.html#_how_can_i_sort_endpoints_alphabetically – Sami Oct 28 '22 at 00:42
  • 1
    `operations-sorter` sorts the endpoints from one class, `tags-sorter` sort the classes inside swagger ui. With both of them you sort first the class title, and inside each one, the endpoints. – King Midas Jul 10 '23 at 11:16
8

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();
}

There are 2 possible values:

  • OperationsSorter.ALPHA - sorts API endpoints alphabetically by path
  • OperationsSorter.METHOD - sorts API endpoints alphabetically by method

OperationsSorter.METHOD is what you are looking for.


Alternative by using operationOrdering():

@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());
            }
        })
}

However, this does not work because of a bug in Springfox which seems to be still active (Operation ordering is not working).

Vladas Maier
  • 2,054
  • 2
  • 22
  • 34
4
@Bean
public UiConfiguration uiConfig() {
    return UiConfigurationBuilder
            .builder()
            .operationsSorter(OperationsSorter.METHOD)
            .build();
}

This works for me. I'm using Spring Boot 2.2.0.M6, Swagger UI 2.9.2

Tran Dinh
  • 41
  • 2
0

springdoc.swagger-ui.operations-sorter does not work in v3/api-docs.

For alphabetical ordering in v3/api-docs use :

springdoc.writer-with-order-by-keys=true
Shapur
  • 498
  • 7
  • 17
0

Add this to your application.properties file

springdoc.swagger-ui.operationsSorter=method
springdoc.swagger-ui.tagsSorter=alpha

Result : enter image description here

Bilal
  • 1,254
  • 13
  • 14