San Jaisy
San Jaisy

Reputation: 17138

Swagger Api Documentation in Spring API Gateway

w

I have the above architecture in the project. Product, Order, Payment Microservice is a Rest API which currently has swagger integration, but now the flow is changed I can't expose the Microservice Rest API now all the REST API calls is been made from API Gateway.

Is there any way to document the API through API gateway in swagger or what is the best practice for this case.

This is the routing configuration in API Gateway Spring boot

@Bean
    public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(r -> r.path("/order/**")
                        .filters(f -> f.hystrix(option -> option.setName("order-service").
                                setFallbackUri("forward:/orderFallBack")))
                        .uri("lb://ORDER-SERVICE")
                        .id("order-service"))

                .route(r -> r.path("/payment/**")
                        .filters(f -> f.hystrix(option -> option.setName("payment-service")
                                .setFallbackUri("forward:/paymentFallBack")))
                        .uri("lb://PAYMENT-SERVICE")
                        .id("payment-service"))

                .route(r -> r.path("/product/**")
                        .filters(f -> f.hystrix(option -> option.setName("product-service")
                                .setFallbackUri("forward:/productFallBack")))
                        .uri("lb://PRODUCT-SERVICE")
                        .id("product-service"))
                .build();
    }

Swagger configuration in Order Microservice Project

@Configuration
public class SwaggerConfiguration {
    @Bean
    public Docket orderApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(getApiInfo());
    }

    //create api metadata that goes at the top of the generated page
    private ApiInfo getApiInfo() {
        return new ApiInfoBuilder()
                .title("Fete Bird Order Microservice")
                .version("1.0")
                .description("API for managing Fete Bird Order Microservice.")
                .license("Fete Bird License Version 1.0")
                .build();
    }
}

enter image description here

Upvotes: 9

Views: 15501

Answers (2)

Artur Yolchyan
Artur Yolchyan

Reputation: 11

Make sure to have the below dependencies in your services: product, payment, order and api-gateway:

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${swagger.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${swagger.version}</version>
    </dependency>

Add @EnableSwagger2 annotation in all of your services.

Add zuul proxy dependencies in your API gateway project. This should route the traffic from api-gateway swagger to your other services.

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>

Add @EnableZuulProxy annotation in api-gateway.

Then, put this config in api-gateway and things should work.

@Primary
@Configuration
public class Swagger2Config implements SwaggerResourcesProvider {

@Autowired
private RouteLocator routeLocator;

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

    routeLocator.getRoutes().forEach(route -> {
        resources.add(swaggerResource(route.getId(), route.getFullPath().replace("**", "v2/api-docs"), "1.0"));
    });

    return resources;
}

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

}

When you will land in your api-gateway swagger page, in the top right you will see a select option for product, payment and order services. Select any of them and try to use the APIs.

Upvotes: 0

Mubaraka Bharucha
Mubaraka Bharucha

Reputation: 206

There is one common practice to make individual swagger endpoints available from gateway itself. I have seen this being done in many production level projects.

For example, for Order service the documentation would be at :

http://gateway-url/order-service/swagger-ui.html

Similar approach could be followed for other micro-services.

Upvotes: -1

Related Questions