14

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.

SSK
  • 3,444
  • 6
  • 32
  • 59

4 Answers4

7

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.

SSK
  • 3,444
  • 6
  • 32
  • 59
  • how to add this custom logic, example please? autoconfigure does not provide "springfox.documentation.swagger-ui.config-url" proprerty. inspecting UiConfiguration also can't find suitable props. – Simon Logic Feb 15 '21 at 13:21
3

this is not about the redirect, as it works to just open to http://localhost:8080/swagger-ui/index.html. But some answers are about disabling the petshop and configUrl. Supplying configUrl is not working anymore for a autoconfigured springdoc. Overwriting the url, config-url (if needed) and default url works with the following application.yml:

springdoc:
  swagger-ui:
    url: "/v3/api-docs"
    disable-swagger-default-url: true

or you can use the topics plugin:

springdoc:
  swagger-ui:
    disable-swagger-default-url: true
    urls:
      - url: "/v3/api-docs"
        name: "myService"
1

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.

  • Did you find a way to make `/v3/api-docs` default url? – Denis Mar 09 '21 at 17:31
  • 3
    Haven't found a way yet. Someone suggested using `springdoc.swagger-ui.disable-swagger-default-url=true` to disable the "Swagger Petstore" page. But I still need to access `/swagger-ui/index.html` then search for `/v3/api-docs`. – Milo Felipe Mar 11 '21 at 02:36
-2

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;
            }
        }
    }
}
SSK
  • 3,444
  • 6
  • 32
  • 59
jsmu9192
  • 1
  • 1
  • This approach uses the interceptor to replace all the request coming for /index.html to convertion as per your overwriteDefaultUrl(html). But this is not clean as unwanted response comes when you load other "/index.html" files – kanishk verma Feb 18 '22 at 06:56