【问题标题】:Swagger UI redirecting to /swagger-ui/index.html?configUrl=/v3/api-docs/swagger-configSwagger UI 重定向到 /swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config
【发布时间】:2025-04-15 16:45:01
【问题描述】:

我正在使用springdoc-openapi-ui,当我点击http://localhost:8080/swagger-ui.html URL 时,它总是重定向到http://localhost:8080/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config。有什么方法可以停止此重定向并在http://localhost:8080/swagger-ui.html 上加载招摇。

【问题讨论】:

    标签: spring-boot swagger swagger-ui springdoc-openapi-ui


    【解决方案1】:

    这与重定向无关,因为它只需要打开 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"
    

    【讨论】:

      【解决方案2】:

      我找到了解决此问题的帖子。扫描修改index.html,将petStore URL替换为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;
                  }
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 你有没有办法让/v3/api-docs成为默认网址?
        • 还没找到办法。有人建议使用springdoc.swagger-ui.disable-swagger-default-url=true 禁用“Swagger Petstore”页面。但我仍然需要访问/swagger-ui/index.html,然后搜索/v3/api-docs
        【解决方案4】:

        我在Github 网站上问过同样的问题。下面提供了链接
        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.

        【讨论】:

        • 如何添加这个自定义逻辑,请举例?自动配置不提供“springfox.documentation.swagger-ui.config-url”属性。检查 UiConfiguration 也找不到合适的道具。