我正在使用springdoc-openapi-ui
,当我点击http://localhost:8080/swagger-ui.html URL 时,它总是重定向到http://localhost:8080/swagger-ui/index.html?configUrl=/v3/api-docs /招摇配置。有什么方法可以停止此重定向并在http://localhost:8080/swagger-ui.html上加载 swagger 。
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.
我也遇到了这个问题,因为我们的应用程序位于网关/负载均衡器和 Docker 后面。我的目标是真正访问 Swagger UI,我的解决方法是/swagger-ui/index.html
直接访问。它加载“Swagger Petstore”。在“探索”字段中,我键入/v3/api-docs
以加载我的应用程序的 API。
这与重定向无关,因为它只需要打开 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"
我找到了解决此问题的帖子。扫描修改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;
}
}
}
}