Search code examples
javaspring-bootswaggerspring-cloud

Swagger API documentation in an API gateway (spring-cloud-starter-gateway) does not work (Whitelabel Error Page)


I am trying to add the Swagger API documentation in a Springboot application that uses the following dependency:

implementation 'org.springframework.cloud:spring-cloud-starter-gateway'

I am using the following configuration:

@OpenAPIDefinition
@Configuration
public class SwaggerConfiguration {
    @Bean
    public OpenAPI baseOpenAPI(){
        return new OpenAPI().info(
                new Info()
                        .title("This is a test")
                        .version("0.1")
        );
    }
}

This is my controller:

@RestController
public class HelloWorldController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}

and my build.gradle:

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.7.11'
    id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}

group = 'com.noob234'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

ext {
    set('springCloudVersion', "2021.0.7")
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
    implementation 'org.springdoc:springdoc-openapi-ui:1.6.15'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'io.projectreactor:reactor-test'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

tasks.named('test') {
    useJUnitPlatform()
}

I suspect that the issue is related to this dependency

implementation 'org.springframework.boot:spring-boot-starter-webflux'

because I tried the same steps without introducing the

implementation 'org.springframework.cloud:spring-cloud-starter-gateway'

dependency and it works when I access this page http://localhost:8082/swagger-ui/index.html#/. I don't get Whitelabel Error Page anymore.

EDIT:

I also tried to add these 2 dependencies:

implementation 'org.springdoc:springdoc-openapi-webflux-ui:1.6.15'
implementation 'org.springdoc:springdoc-openapi-webflux-core:1.6.15'

Instead of this one:

implementation 'org.springdoc:springdoc-openapi-ui:1.6.15'

And added these properties in my application.properties:

spring.application.name=gateway
server.port=8081
springdoc.swagger-ui.path=/swagger-ui.html
springdoc.api-docs.path=/v3/api-docs
springdoc.swagger-ui.enabled=true
spring.cloud.gateway.routes[0].id=swagger-ui
spring.cloud.gateway.routes[0].uri=http://localhost:8081
spring.cloud.gateway.routes[0].predicates[0]=Path=/swagger-ui.html

spring.cloud.gateway.routes[1].id=api-docs
spring.cloud.gateway.routes[1].uri=http://localhost:8081
spring.cloud.gateway.routes[1].predicates[0]=Path=/v3/api-docs

Please tell me if you can reproduce this issue if you follow the steps and what I should do to fix it.


Solution

  • Since api gateway uses webflux, instead of using swagger dependency

    implementation 'org.springdoc:springdoc-openapi-ui:1.6.15'
    

    use swagger webflux dependency

    implementation 'org.springdoc:springdoc-openapi-webflux-ui:1.6.15'
    

    Please find working repo here for reference

    After updating this in build gradle swagger url works fine:

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

    enter image description here