Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot - open api - how to read external swagger api documents using java config

I am generating multiple swagger api doc files in json format on application startup in my spring boot app and storing at location - static/swaggerdoc. I am able to read the file by mentioning the path in application.properties like below

application.properties

springdoc.swagger-ui.urls[0].url=/swaggerdoc/openapi.json
springdoc.swagger-ui.urls[0].name=openapi

springdoc.swagger-ui.urls[1].url=/swaggerdoc/openapi1.json
springdoc.swagger-ui.urls[1].name=openapi1

springdoc.swagger-ui.urls[2].url=/swaggerdoc/openapi2.json
springdoc.swagger-ui.urls[2].name=openapi2

springdoc.swagger-ui.urls[3].url=/swaggerdoc/openapi3.json
springdoc.swagger-ui.urls[3].name=openapi3

Now i have to read the springdoc.swagger-ui.urls dynamically on application start instead of reading static paths from properties file. Appreciate for any help.

I am using below dependency

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

Thanks you.

like image 488
Amit Jadhav Avatar asked Jun 28 '26 07:06

Amit Jadhav


1 Answers

I am able to resolve this issue by adding below bean definition in my config class to load the external documents dynamically using java config.

@Primary
@Bean
public Set<SwaggerUrl> apis(SwaggerUiConfigProperties swaggerUiConfig) {
    Set<SwaggerUrl> swaggerUrlSet = new HashSet<>();
    externalDocs.forEach(doc -> {
        String docName = doc.get("docName");
        SwaggerUrl wsResource = new SwaggerUrl(docName, "/swaggerdoc/" + docName + ".json");
        swaggerUrlSet.add(wsResource);
    });
    swaggerUiConfig.setUrls(swaggerUrlSet);
    return swaggerUrlSet;
}
like image 118
Amit Jadhav Avatar answered Jul 01 '26 00:07

Amit Jadhav