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
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.
1 Comment
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
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
/v3/api-docs default url?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.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
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;
}
}
}
}