Difference between revisions of "Swagger tips: show APIs in reverse order"
From DarkWiki
(→Introduction) |
(→Introduction) |
||
| Line 6: | Line 6: | ||
SOLUTION: Reverse the order of the group names when they're presented inside Swagger, so that the most recent version is shown 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. | * "v1.0", "v1.1", "v2.0" will show "v2.0" by default. | ||
| + | |||
| + | [[File:Swagger-spec-screenshot.png]] | ||
==Code== | ==Code== | ||
Revision as of 11:42, 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.
File:Swagger-spec-screenshot.png
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;
}
}