user3853393
user3853393

Reputation: 253

Possibility of swagger to document multiple services running on different ports

I have integrated swagger with my micro services built using Spring boot. Its very cool to document each and every operation of the service running on specific port. But here i want to document all of my different services running on different ports under one roof. So in one document i need all the services. Is it possible with swagger? or is there any other way to achieve this . Please suggest.

Thanks in advance.

 @SpringBootApplication
@ComponentScan
@EnableAutoConfiguration
@EnableSwagger2
public class App 
{
    public static void main( String[] args )
    {
       // System.out.println( "Hello World!" );
        SpringApplication.run(App.class, args);
    }

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() 
                  .apis(Predicates.not(RequestHandlerSelectors.basePackage("com.comcast.BulkSolve.Controller")))
                  .apis(Predicates.not(RequestHandlerSelectors.basePackage("com.comcast.FileProcess.controller")))
                  //.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.security")))
                     .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Single swagger")
                .description("API to retrieve swagger apis")
                .version("2.0.0")
                .build();
    }
}

=================================

public class GatewaySwaggerResourceProvider implements SwaggerResourcesProvider{

     @Autowired
        private SwaggerServicesConfig swaggerServiceList;

        public GatewaySwaggerResourceProvider() {
        }

    @Override
    public List<SwaggerResource> get() {
        // TODO Auto-generated method stub
         List<SwaggerResource> resources = new ArrayList<>();

            List<SwaggerServices> servList=swaggerServiceList.getServiceList();
            for (SwaggerServices swaggerServices : servList) {
                resources.add(swaggerResource(swaggerServices.getName(), swaggerServices.getUrl(),swaggerServices.getVersion()));
            }
            /*swaggerServiceList.getServiceList().forEach(service -> {
                resources.add(swaggerResource(service.getName(), service.getUrl(), service.getVersion()));
            });*/
            return resources;
    }

    private SwaggerResource swaggerResource(String name, String location, String version) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation(location);
        swaggerResource.setSwaggerVersion(version);
        return swaggerResource;
    }

}

=======================================

@Component
@EnableAutoConfiguration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "documentation.swagger")
public class SwaggerServicesConfig {

     List<SwaggerServices> swagger;

        public List<SwaggerServices> getServiceList() {
            return swagger;
        }

        public void setServiceList(List<SwaggerServices> swaggerResources) {
            this.swagger = swaggerResources;
        }

        @EnableConfigurationProperties
        @ConfigurationProperties(prefix = "documentation.swagger.services")
        public static class SwaggerServices {
            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;
            }

            @Override
            public String toString() {
                return "SwaggerServices [name=" + name + ", url=" + url + ", version=" + version + "]";
            }

        }

}

Application.yml

server:
  port: 9060

documentation: 
  baseurl: http://localhost
  swagger: 
    services:   
      - 
        name: SolveDocumentation
        url: http://localhost:9003/v2/api-docs
        version: 2.0
      - 
        name: FileprocessDocumentation
        url: http://localhost:9004/v2/api-docs
        version: 2.0

My out put is as follows

enter image description here

9003 api documentation: enter image description hereenter image description here

Below is Resources i am getting for http://localhost:9060/swagger-resources enter image description here

Below is the swagger-ui getting only one api documentation: enter image description here

Upvotes: 3

Views: 5269

Answers (1)

mike
mike

Reputation: 435

I had a similar problem a while ago, when I tried to consolidate swagger of multiple microservices. please refer to this example Consolidated swagger

Upvotes: 0

Related Questions