wanted to access the other microservices api documentation though single port

54 views Asked by At
spring.datasource.url=jdbc:mysql://localhost:3306/employee_db
spring.datasource.username=root
spring.datasource.password=root123
server.port=8081
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.application.name=employee-service
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
springdoc.show-actuator=true
springdoc.api-docs.path=employee-service/v3/api-docs

this is my employee service properites file and using as dependency

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

i am able to access localhost:8081/employee-service/v3/api-docs and http://localhost:8081/swagger-ui/index.html

spring.datasource.url=jdbc:mysql://localhost:3306/department_db
spring.datasource.username=root
spring.datasource.password=root123
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.application.name=department-service
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
springdoc.api-docs.path=department-service/v3/api-docs

this is my department-service properties file and using same depdency of employee and i am able to access localhost:8080/department-service/v3/api-docs and http://localhost:8080/swagger-ui/index.html

this is my gateway properties spring.application.name=api-gateway

server.port=8082
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
management.endpoints.web.exposure.include=*
spring.cloud.gateway.discovery.locator.enabled=true
##Routes for employee service
spring.cloud.gateway.routes[0].id=employee-service
spring.cloud.gateway.routes[0].uri=lb://employee-service
spring.cloud.gateway.routes[0].predicates[0]=Path=/employee-service/**
##Routes for department service
spring.cloud.gateway.routes[1].id=department-service
spring.cloud.gateway.routes[1].uri=lb://department-service
spring.cloud.gateway.routes[1].predicates[0]=Path=/department-service/**
spring.cloud.gateway.routes[2].id=openapi
spring.cloud.gateway.routes[2].uri=lb://api-gateway
spring.cloud.gateway.routes[2].predicates[0]=Path=/v3/api-docs/**
spring.cloud.gateway.routes[2].filters[0]=RewritePath=/v3/api-docs/(?<path>.*), /${path}/v3/api-docs
#[email protected]@
springdoc.swagger-ui.use-root-path=true

i am able to access localhost:8082/employee-service/v3/api-docs localhost:8082/department-service/v3/api-docs

and i wanted to access the UI swagger using the dependency localhost:8082/swagger-ui.html or localhost:8082/swagger-ui/index.html but getting white label error page

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

this is my gateway class

@SpringBootApplication
@EnableDiscoveryClient
public class ApiGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiGatewayApplication.class, args);
    }

    @Bean
    @Lazy(false)
    public List<GroupedOpenApi> apis(RouteDefinitionLocator locator) {
        List<GroupedOpenApi> groups = new ArrayList<>();
        List<RouteDefinition> definitions = locator.getRouteDefinitions().collectList().block();
        for (RouteDefinition definition : definitions) {
            System.out.println("id: " + definition.getId() + "  " + definition.getUri().toString());
        }
        definitions.stream().filter(routeDefinition -> routeDefinition.getId().matches(".*-service")).forEach(routeDefinition -> {
            String name = routeDefinition.getId().replaceAll("-service", "");
            GroupedOpenApi.builder().pathsToMatch("/" + name + "/**").group(name).build();
        });
        return groups;
    }
}
@Component
public class ContextPathRewritePathGatewayFilterFactory extends RewritePathGatewayFilterFactory {

    @Override
    public GatewayFilter apply(Config config) {
        String replacement = config.getReplacement().replace("$\\", "$");
        return (exchange, chain) -> {
            ServerHttpRequest req = exchange.getRequest();

            addOriginalRequestUrl(exchange, req.getURI());
            String path = req.getURI().getRawPath();

            String newPath = path.replaceAll(config.getRegexp(), replacement);
            ServerHttpRequest request = req.mutate().path(newPath).contextPath("/").build();

            exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, request.getURI());

            return chain.filter(exchange.mutate().request(request).build());
        };
    }

}

let me know how i can access the swagger -ui version of gateway and what changes i have to do for this .

0

There are 0 answers