简体   繁体   中英

APIs segmentation on Swagger

I have a question regarding to microservice architecture app swagger. The app is generated with JHipster. Since my backend app has a lot of endpoints, loading those endpoints into swagger is impossible (the whole gateway app gets stuck). So, my idea is to make some kind of endpoints groups on Swagger (filters). For now, API item from dropdown list on gateway app opens Swagger UI with two items, default (/v2/api-docs) and service (/service/v2/api-docs)item. Click on the second item gets complete gateway app stuck by loading all the endpoints. So, I tried to make multiple dropdown items instead of one item (service (service/v2/api-docs)). I followed this tutorial : https://piotrminkowski.wordpress.com/2017/04/14/microservices-api-documentation-with-swagger2/ So, in my gateway app, I have GatewaySwaggerResourceProvider:

@Override
public List<SwaggerResource> get() {
    List<SwaggerResource> resources = new ArrayList<>();

    //Add the default swagger resource that correspond to the gateway's own swagger doc
    resources.add(swaggerResource("default", "/v2/api-docs"));

    //Add the registered microservices swagger docs as additional swagger resources
    List<Route> routes = routeLocator.getRoutes();
    routes.forEach(route -> {
        resources.add(swaggerResource(route.getId(), route.getFullPath().replace("**", "v2/api-docs")));
    });

    return resources;
}

In my main class of the gateway I have put:

@Bean
UiConfiguration uiConfig() {
    return new UiConfiguration("validatorUrl", "list", "alpha", "schema",
        UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L);
}

In my microservice app (backend app) main class, I have put Docket @Bean which filters only endpoints from specific package:

@Bean
public Docket api() throws IOException {
    return new Docket(DocumentationType.SWAGGER_2)
        .groupName("product")
        .select()
        .apis(RequestHandlerSelectors.basePackage("kango.web.rest"))
        .paths(PathSelectors.ant("/product-types/*"))
        .build()
        .apiInfo(apiInfo());
}

After this, I restarted both applications and the Swagger still shows initial two items in dropdown list. What I need is to have multiple items with similar endpoints groups.

I have lost plenty of time, and did a good research before asking, so please, help me...

you can define tags under @Api to group them. but it wont give you a different swagger json file. in order to get separate json per segregation, here is an example

in this example i am creating SpringFox Docket Bean dynamically by Scanning @RestController and group them per Controller Class. you can have your own grouping strategy, by defining custom Annotation and scan them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM