15

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.

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
4

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 Comment

these two properties are what you need! url and disable-swagger-default-url. worked for me! thanks!
2

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.

2 Comments

Did you find a way to make /v3/api-docs default url?
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.
0

I had same problem with redirect to /swagger-ui/index.html with status code 404. In my kotlin project. My problem was that I used custom configuration extends WebMvcConfigurationSupport:

@Configuration
class CustomConfig() : WebMvcConfigurationSupport() {...}

So I had to update that config like this and it works! :)

@Configuration
class CustomConfig(
    private val swaggerIndexTransformer: SwaggerIndexTransformer
) : WebMvcConfigurationSupport() {

    public override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
        registry.addResourceHandler("/swagger-ui*/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/")
            .resourceChain(false)
            .addTransformer(swaggerIndexTransformer)

        super.addResourceHandlers(registry)
    }
}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-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;
            }
        }
    }
}

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.