我正在使用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 上大摇大摆地加载。
我在网站上问过同样的问题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.
这与重定向无关,因为它只适用于向 http://localhost:8080/swagger-ui/index.html 开放。但一些答案是关于禁用宠物店和配置网址。对于自动配置的 springdoc,提供 configUrl 不再有效。覆盖 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"
我也遇到了这个问题,因为我们的应用程序位于网关/负载均衡器后面和 Docker 上。我的目标是真正访问 Swagger UI,我的解决方法是直接访问/swagger-ui/index.html
。它加载了"招摇宠物店"。在"浏览"字段中,我键入/v3/api-docs
以加载应用程序的 API。
我找到了一个解决这个问题的帖子。扫描并修改索引.html以将宠物商店网址替换为 apiDoc 网址
@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;
}
}
}
}