Search code examples
javaspring-bootspring-mvcopenapiredoc

Redoc failed to generate html template (Something went wrong... Failed to fetch)


I am trying to integrate Redoc as an alternative to swagger for better personalised documentation with springboot 2.7 (java 11)after searching for the cause of the error , i found several answers that i tried : API Documentation with ReDoc and Spring boot (am i doing something wrong ?) also is it possible to preview the documentation page in intellij or in the navigator without the need to run the spring application since the app is not runnable locally.

error details :

Something went wrong... Failed to fetch Stack trace Error: Failed to fetch at t.BaseResolver. (https://cdn.jsdelivr.net/npm/redoc@latest/bundles/redoc.standalone.js:2:31063) at Generator.throw () at s (https://cdn.jsdelivr.net/npm/redoc@latest/bundles/redoc.standalone.js:2:28890)

ReDoc Version: 2.1.3 Commit: b2d8e0f

application.properties :

 server.port=4949

 springdoc.api-docs.path=/api-docs
 spring.web.resources.static-locations=classpath:/custom/


#springdoc.api-docs.enabled=true
#springdoc.swagger-ui.path=/swagger-ui.html
#springdoc.api-docs.path=/api-docs
#springdoc.swagger-ui.enabled=true
#springdoc.swagger-ui.operationsSorter=method
#springdoc.swagger-ui.tryItOutEnabled=true
#springdoc.swagger-ui.filter=false
#springdoc.swagger-ui.tagsSorter=alpha
#springdoc.swagger-ui.validatorUrl=none
#springdoc.swagger-ui.defaultModelRendering=model
#springdoc.swagger-ui.docExpansion=full

my configuration :

@Configuration
@EnableWebMvc
public class Static_ResourceHandler implements WebMvcConfigurer {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

    registry
            .addResourceHandler("/static/**") 
            .addResourceLocations("classpath:/static/") // Default Static Loaction
            .setCachePeriod( 3600 )
            .resourceChain(true) //
            .addResolver(new GzipResourceResolver())
            .addResolver(new PathResourceResolver()); 

    registry
            .addResourceHandler("/templates/**") 
            .addResourceLocations("classpath:/resources/static/");


}

}

i didnt add a css file ,i am only trying to get the index.html to work for now (i will appriciate if someone could help with the placement and config of the css file also !)

index.html (ressouces/static/index.html)

 <!DOCTYPE html>
 <html>
 <head>
 <title>Redoc</title>
 <meta charset="utf-8"/>
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <link href="https://fonts.googleapis.com/css? 
 family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">


<style>
    body {
        margin: 0;
        padding: 0;
    }
</style>
</head>
<body>

<redoc spec-url='http://localhost:4949/api-docs'></redoc>
<script src="https://cdn.jsdelivr.net/npm/redoc@latest/bundles/redoc.standalone.js"> 
 </script>
 </body>
 </html>

springboot dependency :

    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
        <version>2.2.0</version>
    </dependency>

updates :

i am trying to integrate Redoc with a new springboot app (MVC) and i am getting this error :

enter image description here

and this config didnt solve the cross origin problem

@Configuration
@EnableWebMvc
public class Static_ResourceHandler implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/v3/api-docs")
            .allowedOrigins("*")
            .allowedMethods("GET") methods as needed
            .allowCredentials(true);
}

}


Solution

  • So i found the solution and no you dont need a running Node.js server for Redoc to works popperly:

    step one :

    you cant allow all origins in your config its only works if your frontend is on the same envirement :

    @Configuration
    public class Static_ResourceHandler implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/v3/api-docs")
                .allowedOrigins("http://localhost:63342") // changed code
                .allowedMethods("GET")
                .allowCredentials(true);
    }
    

    }

    Step 2 :

    I changed the dependency to openapi-ui so no need for WebMvcConfigurer anymore :

        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.6.11</version>
        </dependency>
    

    step 3 :

    changed spec-url to swagger url .

     <redoc spec-url='http://localhost:4949/v3/api-docs' ></redoc>