I’m using Spring Cloud Gateway to route requests to several microservices, each with its own Swagger documentation. My goal is to access all the Swagger documentation via the Gateway’s Swagger UI. However, I am encountering an issue where the Swagger UI static resources are not being routed correctly.
Gateway Configuration
Here is my current application.yml configuration for the Gateway:
spring:
application:
name: gateway
cloud:
gateway:
routes:
- id: product
uri: ${system-configuration.endpoints.product-service}
predicates:
- Path=/api/products/**
- id: recommendation
uri: ${system-configuration.endpoints.recommendation-service}
predicates:
- Path=/api/recommendations/**
- id: review
uri: ${system-configuration.endpoints.review-service}
predicates:
- Path=/api/reviews/**
springdoc:
api-docs:
enabled: true
swagger-ui:
enabled: true
path: /swagger-ui.html
config-url: /v3/api-docs/swagger-config
urls:
- url: /v3/api-docs
name: API Gateway Service
- name: product-service
url: /api/products/v3/api-docs
- name: recommendation-service
url: /api/recommendations/v3/api-docs
- name: review-service
url: /api/reviews/v3/api-docs
logging:
level:
org.springframework.cloud.gateway: TRACE
Microservice Configuration
Each microservice has a similar Swagger configuration. Here’s an example for the product-service:
spring:
application:
name: product-service
springdoc:
api-docs:
path: /api/products/v3/api-docs
swagger-ui:
path: /api/products/swagger-ui.html
Issue
When I try to access the Swagger UI through the Gateway (e.g., http://localhost:8080/swagger-ui.html), I get the following error in the Gateway logs:
No RouteDefinition found for [Exchange: GET http://localhost:8080/webjars/swagger-ui/index.html]
Gateway Logs:
2024-08-03T22:08:50.590+03:00 TRACE 16148 --- [gateway] [ctor-http-nio-4] o.s.c.g.h.RoutePredicateHandlerMapping : No RouteDefinition found for [Exchange: GET http://localhost:8080/webjars/swagger-ui/index.html]
2024-08-03T22:08:50.824+03:00 TRACE 16148 --- [gateway] [ctor-http-nio-4] o.s.c.g.f.WeightCalculatorWebFilter : Weights attr: {}
2024-08-03T22:08:50.825+03:00 TRACE 16148 --- [gateway] [ctor-http-nio-4] o.s.c.g.h.p.PathRoutePredicateFactory : Pattern "[/api/products/**, /api/products/v3/api-docs]" does not match against value "/webjars/swagger-ui/$%7Bserver.servlet.contextPath%7D/v3/api-docs/swagger-config"
2024-08-03T22:08:50.825+03:00 TRACE 16148 --- [gateway] [ctor-http-nio-4] o.s.c.g.h.p.PathRoutePredicateFactory : Pattern "[/api/recommendations/**]" does not match against value "/webjars/swagger-ui/$%7Bserver.servlet.contextPath%7D/v3/api-docs/swagger-config"
2024-08-03T22:08:50.825+03:00 TRACE 16148 --- [gateway] [ctor-http-nio-4] o.s.c.g.h.p.PathRoutePredicateFactory : Pattern "[/api/reviews/**]" does not match against value "/webjars/swagger-ui/$%7Bserver.servlet.contextPath%7D/v3/api-docs/swagger-config"
2024-08-03T22:08:50.825+03:00 TRACE 16148 --- [gateway] [ctor-http-nio-4] o.s.c.g.h.RoutePredicateHandlerMapping : No RouteDefinition found for [Exchange: GET http://localhost:8080/webjars/swagger-ui/$%7Bserver.servlet.contextPath%7D/v3/api-docs/swagger-config]