Swagger UI 重定向到 /swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config

Swagger UI redirecting to /swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config

我正在使用 springdoc-openapi-ui,当我点击 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 时。

我在 Github 网站上也问过同样的问题。 Link 下面提供
https://github.com/springdoc/springdoc-openapi/issues/742#issue-642810354
得到其中一位贡献者的回复

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.

我也遇到了这个问题,因为我们的应用程序在 gateway/load 平衡器后面并且在 Docker 上。我的目标是真正访问 Swagger UI,我的解决方法是直接访问 /swagger-ui/index.html。它加载“Swagger Petstore”。在“探索”字段中,我键入 /v3/api-docs 以加载我的应用程序的 API。

我找到了 post 解决方法。扫描并修改 index.html 替换 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;
            }
        }
    }
}

这与重定向无关,因为它只打开 http://localhost:8080/swagger-ui/index.html。但是有些答案是关于禁用 petshop 和 configUrl 的。提供 configUrl 不再适用于自动配置的 springdoc。覆盖 url、config-url(如果需要)和默认 url 适用于以下 application.yml:

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

或者您可以使用主题插件:

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