'Configure Open API 3 - SpringBoot with multiple controller classes

Am trying to configure swagger 3 spring documentation for my springboot application. I have 3 controller classes with 1 functional endpoint method each. If I open the swagger documentation I can see only the first class's endpoint details in Swagger UI.

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.4</version>
    <type>pom</type>
</dependency>

    <dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-webflux-core</artifactId>
    <version>1.4.6</version>
</dependency>
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-webflux-ui</artifactId>
    <version>1.4.6</version>
</dependency>

And my controller class looks like this.

@Configuration
public class CompanyOneClass {
    
    @RouterOperations({ @RouterOperation(path = "/getAllPersons", beanClass = PersonService.class, beanMethod = "getAll"),
      @RouterOperation(path = "/getPerson/{id}", beanClass = PersonService.class, beanMethod = "getById"),
      @RouterOperation(path = "/createPerson", beanClass = PersonService.class, beanMethod = "save"),
      @RouterOperation(path = "/deletePerson/{id}", beanClass = PersonService.class, beanMethod = "delete") })
@Bean
public RouterFunction<ServerResponse> companyOneRoute(CompanyOneHandler companyOneHandler) {
  
   return route().path("/company-one", a1->a1.nest(accept.APPLICATION_JSON),
                                        a2->a2.GET("/getAllPersons",companyOneHandler::findAll)
                                        .GET("/{id}",companyOneHandler::findOne))
                                        .POST("",companyOneHandler::createPerson)
                                        .DELETE("/{id}",companyOneHandler::deletePerson)).build();
  
}
}

@Configuration
public class CompanyTwoClass {
    
    @RouterOperations({ @RouterOperation(path = "/getAllPersons", beanClass = PersonService.class, beanMethod = "getAll"),
      @RouterOperation(path = "/getPerson/{id}", beanClass = PersonService.class, beanMethod = "getById"),
      @RouterOperation(path = "/createPerson", beanClass = PersonService.class, beanMethod = "save"),
      @RouterOperation(path = "/deletePerson/{id}", beanClass = PersonService.class, beanMethod = "delete") })
@Bean
public RouterFunction<ServerResponse> companyTwoRoute(CompanyTwoHandler companyTwoHandler) {
  
   return route().path("/company-two", a1->a1.nest(accept.APPLICATION_JSON),
                                        a2->a2.GET("/getAllPersons",companyTwoHandler::findAll)
                                        .GET("/{id}",companyTwoHandler::findOne))
                                        .POST("",companyTwoHandler::createPerson)
                                        .DELETE("/{id}",companyTwoHandler::deletePerson)).build();
  
}
}

@Configuration
public class CompanyThreeClass {
    
    @RouterOperations({ @RouterOperation(path = "/getAllPersons", beanClass = PersonService.class, beanMethod = "getAll"),
      @RouterOperation(path = "/getPerson/{id}", beanClass = PersonService.class, beanMethod = "getById"),
      @RouterOperation(path = "/createPerson", beanClass = PersonService.class, beanMethod = "save"),
      @RouterOperation(path = "/deletePerson/{id}", beanClass = PersonService.class, beanMethod = "delete") })
@Bean
public RouterFunction<ServerResponse> companyThreeRoute(CompanyThreeHandler companyThreeHandler) {
     return route().path("/company-three", a1->a1.nest(accept.APPLICATION_JSON),
                                        a2->a2.GET("/getAllPersons",companyThreeHandler::findAll)
                                        .GET("/{id}",companyThreeHandler::findOne))
                                        .POST("",companyThreeHandler::createPerson)
                                        .DELETE("/{id}",companyThreeHandler::deletePerson)).build();
}
}

Only similarity is that all these 3 controllers have similar endpoint URLs but route to different handlers. Just for FYI I use java 11.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source