Uncaught ReferenceError: SwaggerUIBundle is not defined at window.onload

Viewed 6098
2 Answers

I faced the same "SwaggerUIBundle is not defined" with a blank page. Also the problem only on server and not local. And it also had worked in the past. Then when I ctrl+f5 the local swagger page, it then also appeared local.

For us it appeared the Configuration order was swapped. For me the fix was moving app.UseRouting() after the swagger init

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{                                    
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "Procedure Service V1");
        c.RoutePrefix = "";
    });
    app.UseRouting();

I am using org.springdoc:springdoc-openapi-ui:1.6.9 version and was facing this problem.

I checked https://www.npmjs.com/package/swagger-ui and added maven dependency for a lower swagger-ui version 3.0.21

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>swagger-ui</artifactId>
    <version>3.0.21</version>
</dependency>

I found that the "swagger-ui/index.html" does not reference "swagger-initializer.js" , instead its source code is present in index.html itself (this the boiler-plate referring to pet store)

So in my Spring boot classpath resources/public folder, I copied the older swagger dist index.html file (which has the initializer code) and did the following changes

  1. Changed the url part to point to my api-docs url. (e.g. /v3/api-docs)
  2. Added "swagger-ui" to the src references, so that the swagger bundles/js will be called successfully from my index.html
<script charset="UTF-8" src="./swagger-ui/swagger-ui-bundle.js"></script>
<script charset="UTF-8" src="./swagger-ui/swagger-ui-standalone-preset.js"> </script>
  1. When I accessed index.html - http://{server}:{port}/index.html- I could access my local Open API doc.
Related