Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I aggregate microservices swaggers into a single swagger

I am trying to generate a single swagger in my microservices project, aggregating all services swaggers into a single one, in the Api Gateway. In order to achieve this, I am following the next tutorial https://objectpartners.com/2017/09/28/aggregate-services-into-a-single-swagger

The problem here is that, when I try to set absolute URLs, the output I am receiving is Failed to load API definition. undefined http://localhost:8070/apihttp://localhost:8081/api/v2/api-docs where localhost:8070/api is the base URL for the api gateway, and localhost:8081/api/v2/api-docs is the docs URL of the swagger of the microservice.

Here is my code:

SwaggerConfiguration

package com.rfd.apigateway.swagger;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@ConfigurationProperties(prefix = "swagger")
public class SwaggerConfiguration {

    private List<Resource> resources;

    public List<Resource> getResources() {
        return resources;
    }

    public void setResources(List<Resource> resources) {
        this.resources = resources;
    }
}

Resource

package com.rfd.apigateway.swagger;

public class Resource {
    private String name;
    private String url;
    private String version;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }
}

DocumentationController

package com.rfd.apigateway.swagger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

@Component
@Primary
@EnableAutoConfiguration
public class DocumentationController implements SwaggerResourcesProvider {

    private SwaggerConfiguration swaggerConfiguration;

    @Autowired
    public DocumentationController(SwaggerConfiguration swaggerConfiguration){
        this.swaggerConfiguration = swaggerConfiguration;
    }

    @Override
    public List get() {
        List resources = new ArrayList<>();
        for(Resource resource : this.swaggerConfiguration.getResources()){
            resources.add(createSwaggerResource(resource));
        }

        return resources;
    }

    private SwaggerResource createSwaggerResource(Resource resource) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(resource.getName());
        swaggerResource.setUrl(resource.getUrl());
        swaggerResource.setSwaggerVersion(resource.getVersion());
        return swaggerResource;
    }
}

Finally, the application.yml

swagger:
  resources:
    - name: transactions
      url: http://localhost:8081/api/v2/api-docs
      version: 1.0
    - name: payments
      url: http://localhost:8083/api/v2/api-docs
      version: 1.0

And a couple of images that can help to understand the problem:

Api Gateway Swagger URL Api gateway swagger URL

Microservice api-docs URL Microservice api-docs URL

like image 751
DarthRoman Avatar asked Feb 06 '18 15:02

DarthRoman


People also ask

How do I add multiple API to swagger?

For example in spring (springfox-swagger) you need just to put the same tag on multiple API classes and it will merge them in one group in the swagger UI. @Api(value = "First API", tags = {"first-api"}) public class FirstApi { ... } @Api(tags = {"first-api"}) public class SecondApi { ... }


1 Answers

The problem here was just the springfox version... I tried to downgrade it from 2.8.0 to 2.7.0 and it worked like a charm. It seems it is a recognized bug, as you can see here: https://github.com/springfox/springfox/issues/2235

I also had to enable cors in microservices but that is another problem.

like image 191
DarthRoman Avatar answered Oct 23 '22 11:10

DarthRoman