I've created a simple application with controller, exposed via Swagger3.
It's working fine on its own, I can access and invoke via http://localhost:8081/swagger-ui/index.html
.
However if I add the Spring Cloud Gateway on top, I couldn't access Swagger, it has the banner but body is showing "Failed to load remote configuration." .
Upon inspection the log is showing
*"GET http://localhost:8080/v3/api-docs/swagger-config 404 (Not Found)"*
*"undefined /v3/api-docs/swagger-config"*
http://localhost:8080/api/servicea/swagger-ui/index.html
On a side note, the service could be invoked fine through gateway, i.e. curl -X "GET" http://localhost:8080/api/servicea/test
.
I believe I missed some configuration but I couldn't identify where. The simplified code is at https://github.com/sonic-air-swimmer/SpringCloudGatewaySwagger
Can someone please tell me what else should I configure?
UPDATE Finally able to identify the mistake, 2 configurations to make it works:
- Removing StripPrefix configuration on the Gateway
- Adding server.servlet.context-path on ServiceA
It was caused by the StripPrefix hence when the ServiceA Swagger redirects it got error 404.
UPDATE2 Although the previous way would work, it is not ideal as it's coupling the setting of services with the API Gateway. For example, if we decided to change the prefix for a particular service at API Gateway, the corresponding service's context path needs to be aligned as well.
Therefore the correct way should be following the guide at https://springdoc.org/faq.html#how-can-i-deploy-springdoc-openapi-ui-behind-a-reverse-proxy
Take note that it might appear to be not working because of the browser/springdoc cache. In that case just add springdoc.cache.disabled=true as per solution in https://github.com/springdoc/springdoc-openapi/issues/331