How to add swagger to spring cloud gateway microservices application

 I know it is easy to add swagger to a spring boot ,Or spring MVC application. But ,what if your APIs runs behind a gateway!

The following steps illustreates how configure swagger to run behind gateway

Step 1: Updates on the Microservices applications

1- In the microservice project , add "io.springfox" maven to your pom.xml

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

2- in the SpringBootApplication class add annotation @EnableSwagger2 and add this bean

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)  
          .select()                                  
          .apis(RequestHandlerSelectors.any())              
          .paths(PathSelectors.any())                          
          .build();                                           
    }

 

save and run

 

Step 2 : Modification on the Spring cloud gateway application

1- add the same swagger dependency to the pom.xml file

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

 2- add routing to enable running swagger-ui and swagger-api

open the application.properties file and add two routes like this

# crm-operations/swagger-ui
spring.cloud.gateway.routes[2].id=sys-owner-app-swagger-ui
spring.cloud.gateway.routes[2].uri=lb://sys-owner-app
spring.cloud.gateway.routes[2].predicates[0]=Path=/sys-owner-app/swagger-resources/**
spring.cloud.gateway.routes[2].predicates[1]=Method=GET
spring.cloud.gateway.routes[2].filters[0]=RemoveRequestHeader=Cookie
spring.cloud.gateway.routes[2].filters[1]=RewritePath=/sys-owner-app/(?<segment>.*),/$\{segment}
# crm-operations/swagger-webjars
spring.cloud.gateway.routes[3].id=sys-owner-app-swagger-jars
spring.cloud.gateway.routes[3].uri=lb://sys-owner-app
spring.cloud.gateway.routes[3].predicates[0]=Path=/sys-owner-app/webjars/**
spring.cloud.gateway.routes[3].predicates[1]=Method=GET
spring.cloud.gateway.routes[3].filters[0]=RemoveRequestHeader=Cookie
spring.cloud.gateway.routes[3].filters[1]=RewritePath=/sys-owner-app/(?<segment>.*),/$\{segment}

# crm-operations/swagger-html
spring.cloud.gateway.routes[4].id=sys-owner-app-swagger-html
spring.cloud.gateway.routes[4].uri=lb://sys-owner-app
spring.cloud.gateway.routes[4].predicates[0]=Path=/sys-owner-app/swagger-ui/**
spring.cloud.gateway.routes[4].predicates[1]=Method=GET
spring.cloud.gateway.routes[4].filters[0]=RemoveRequestHeader=Cookie
spring.cloud.gateway.routes[4].filters[1]=RewritePath=/sys-owner-app/(?<segment>.*),/$\{segment}

example:


 

 Now Time For Testing:

The Swagger URL has been changes

The new URL is

http://<HOST_NAME>:<PORT>/<MICRO-SERVICE-NAME>/swagger-ui/index.html

example

http://localhost:802/sys-owner-app/swagger-ui/index.html

where localhost is thee servername, 802 is the gateway port, "sys-owner-app" is the name of your microservice

change the hostname and the port and the name of thee micro-service


 



Comments

Popular posts from this blog

Another user has changed the row with primary key oracle.jbo.Key[12 ].

weblogic windows JPS-01050: Opening of wallet based credential store failed. Reason java.io.IOException: Failed to lock cwallet.sso.lck

Working With File Throgh WebUtill