Swagger tips: show APIs in reverse order

From DarkWiki
Revision as of 11:28, 20 June 2019 by Apowney (talk | contribs) (Created page with "==Introduction== ==Code== <source lang="java"> import org.springframework.context.annotation.Primary; import org.springframework.core.env.Environment; import org.springframe...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Introduction

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

}