1

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.

Amit Jadhav
  • 83
  • 1
  • 10

2 Answers2

0

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;
}
Amit Jadhav
  • 83
  • 1
  • 10
0

You can also use GroupedOpenApi.builder()

    @Bean
    GroupedOpenApi myGroup() {
            return GroupedOpenApi.builder().group("yourGroupName")
                   .pathsToMatch("yourPath").build();
        }

Here an example of only one grouped API Obviously you can loop on builder and return a Collection of GroupedOpenApi

BELLIL
  • 739
  • 10
  • 23