javaspring-bootswaggeropenapiapi-gateway

How to implement swagger aggregator in API Gateway of Spring Boot Microservices (Cannot show endpoint after selecting any service)


I tried to implement swagger aggregator in my spring boot microservices examples. I can open swagger of api gateway through http://localhost:8080/swagger-ui.html and swagger of category service through http://localhost:8082/swagger-ui.html . I can see all endpoints of category service in http://localhost:8082/swagger-ui.html but even if I can select any service from dropdown , I cannot see any endpoints of defined service in http://localhost:8080/swagger-ui.html.

How can I see its endpoints after selecting any service from dropdown through http://localhost:8080/swagger-ui.html ?

Here is the pom.xml of api-gateway.yml

<dependency>
   <groupId>org.springdoc</groupId>
   <artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
   <version>2.6.0</version>
</dependency>

Here is the pom.xml of category service

<dependency>
  <groupId>org.springdoc</groupId>
  <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
  <version>2.6.0</version>
</dependency>

Here is api-gateway.yml shown below

spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
        - id: user-microservice
          uri: lb://USER-MICROSERVICE
          predicates:
            - Path=/api/users/**
        - id: category-microservice
          uri: lb://CATEGORY-MICROSERVICE
          predicates:
            - Path=/api/categories/**

Here is api-gateway-dev.yml

springdoc:
  swagger-ui:
    path: /swagger-ui.html

Here is category-microservice-dev.yml

springdoc:
  swagger-ui:
    path: /swagger-ui.html

Here is the OpenApiConfig of api-gateway service

import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OpenApiConfig {

    @Bean
    public GroupedOpenApi userMicroserviceApi() {
        return GroupedOpenApi.builder()
                .group("user-microservice")
                .pathsToMatch("/api/users/**")
                .build();
    }

    @Bean
    public GroupedOpenApi categoryMicroserviceApi() {
        return GroupedOpenApi.builder()
                .group("category-microservice")
                .pathsToMatch("/api/categories/**")
                .build();
    }
}

Solution

  • We need to have the below configuration for the individual microservices as a sample

    springdoc:
      swagger-ui:
        path: /swagger-ui.html
        urls:
          - name: category-service
            url: http://localhost:8082/v3/api-docs
          - name: user-service
            url: http://localhost:8081/v3/api-docs
    

    This tells the Swagger UI in your API Gateway to offer dropdown options for these external services. Make sure those services (category and user) expose /v3/api-docs.