Difference between revisions of "Swagger tips: show APIs in reverse order"
From DarkWiki
(Created page with "==Introduction== ==Code== <source lang="java"> import org.springframework.context.annotation.Primary; import org.springframework.core.env.Environment; import org.springframe...") |
(→Introduction) |
||
| (5 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
==Introduction== | ==Introduction== | ||
| + | |||
| + | PROBLEM: When visiting SwaggerUI, dockets with version numbers appear in numerical order, so that the earliest version is shown by default. | ||
| + | * "v1.0", "v1.1", "v2.0" will show "v1.0" by default. | ||
| + | |||
| + | SOLUTION: Reverse the order of the group names when they're presented inside Swagger, so that the most recent version is shown by default. | ||
| + | * "v1.0", "v1.1", "v2.0" will show "v2.0" by default. | ||
| + | |||
| + | [[File:Swagger-screenshot-specs.png]] | ||
==Code== | ==Code== | ||
Latest revision as of 11:47, 20 June 2019
Introduction
PROBLEM: When visiting SwaggerUI, dockets with version numbers appear in numerical order, so that the earliest version is shown by default.
- "v1.0", "v1.1", "v2.0" will show "v1.0" by default.
SOLUTION: Reverse the order of the group names when they're presented inside Swagger, so that the most recent version is shown by default.
- "v1.0", "v1.1", "v2.0" will show "v2.0" by default.
Code
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import springfox.documentation.spring.web.DocumentationCache;
import springfox.documentation.swagger.web.InMemorySwaggerResourcesProvider;
import springfox.documentation.swagger.web.SwaggerResource;
import java.util.Collections;
import java.util.List;
/**
* This is accessed by springfox.documentation.swagger.web.ApiResourceController to get the list of the
* resources provided in this application.
*
* It overrides the default SwaggerResourcesProvider bean (InMemorySwaggerResourcesProvider) to sort
* the resources in reverse. This ensures that the latest API version appears first and is therefore
* presented by default.
*/
@Component
@Primary
public class AnorakSwaggerResourcesProvider extends InMemorySwaggerResourcesProvider {
public AnorakSwaggerResourcesProvider(Environment environment,
DocumentationCache documentationCache) {
super(environment,documentationCache);
}
@Override
public List<SwaggerResource> get() {
List<SwaggerResource> resources = super.get();
resources.sort(Collections.reverseOrder());
return resources;
}
}
