4
votes

I am using springdoc-openapi-ui, when I hit http://localhost:8080/swagger-ui.html URL it is always redirecting to http://localhost:8080/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config. Is there any way to stop this redirect and load swagger on http://localhost:8080/swagger-ui.html instead.

3

3 Answers

5
votes

I have asked the same question on Github site regarding the same. Link is provided below
https://github.com/springdoc/springdoc-openapi/issues/742#issue-642810354
Got the reply from one of the contributor

springdoc-openapi uses an internal redirect to resolve the necessary swagger resources. This the way the library is built.
You use you own version of swagger-ui, so you can add your custom Logic, without having to rely on springdoc-openapi-ui.

1
votes

I also encountered this issue because our app is behind a gateway/load balancer and on Docker. My goal is to really just access the Swagger UI and my workaround is to access /swagger-ui/index.html directly. It loads the "Swagger Petstore". In the "Explore" field, I type /v3/api-docs to load my application's APIs.

-1
votes

I found a post to workaround for this. Scan and modify the index.html to replace petStore URL for apiDoc URL

    @Configuration

    public class DocOpenApiConfiguration implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**/*.html")
                .addResourceLocations("classpath:/META-INF/resources/webjars/")
                .resourceChain(false)
                .addResolver(new WebJarsResourceResolver())
                .addResolver(new PathResourceResolver())
                .addTransformer(new IndexPageTransformer());
    }

    public static class IndexPageTransformer implements ResourceTransformer {

        private String overwriteDefaultUrl(String html) {
            return html.replace("https://petstore.swagger.io/v2/swagger.json",
                    "/v3/api-docs");
        }

        @Override
        public Resource transform(HttpServletRequest httpServletRequest, Resource resource, ResourceTransformerChain resourceTransformerChain) throws IOException {
            if (resource.getURL().toString().endsWith("/index.html")) {
                String html = IOUtils.toString(resource.getInputStream(), StandardCharsets.UTF_8);
                html = overwriteDefaultUrl(html);
                return new TransformedResource(resource, html.getBytes());
            } else {
                return resource;
            }
        }
    }
}