How to expose different swagger APIs (on different ports) on same local server

Viewed 499

I have a project made by four different microservices. Each service is implemented using SpringBoot and documented with SpringDoc (Open Api Swagger 3). I can happly reach all APIs documentation by opening different URLs, such as:

http://localhost:8080/swagger-ui/index.html

http://localhost:8081/swagger-ui/index.html

http://localhost:8082/swagger-ui/index.html

etc...

Assuming that all microservices are running on the same host, I'd like to have the "servers" drop-down menu allowing to switch between the URLs. I tried something like this (for just 2 components):

public OpenAPI initOpenAPI() {
    
    OpenAPI defaultOpenAPI = new OpenAPI();
    
    defaultOpenAPI.info(new Info()
                    .title("My repository")
                    .description("Repository APIs to store my data")
                    .version("v1.0.0")

    Server server_1 = new Server();
    server_1.setDescription("Component 1");
    server_1.setUrl("http://localhost:8080/swagger-ui/index.html");

    Server server_2 = new Server();
    server_1.setDescription("Component 2");
    server_2.setUrl("http://localhost:8081/spider/swagger-ui/index.html");
    
    defaultOpenAPI.setServers(Arrays.asList(server_1, server_2));

    return defaultOpenAPI;
}

And indeed I have all URL in the "servers" drop-down, but when I click on them nothing happens. How can allow to switch from one URL to another by choosing the target URL in the "servers" combo-box?

0 Answers
Related