1

I'm using springdoc-openapi with Kotlin and WebFlux.fn.
I wanted to use @RouterOperation annotation at every path in CoRouterFunctionDsl but I couldn't.

@Configuration
class UserRouter(private val userHandler: UserHandler) {
    // @RouterOperations annotation works here.
    @Bean
    fun userRouter = coRouter {
        ("/v1").nest {
            // I want to use @RouterOperation annotation here.
            GET("/users", userHandler::getUsers)

            // I want to use @RouterOperation annotation here.
            GET("/users/{userId}", userHandler::getUserById)

            // I want to use @RouterOperation annotation here.
            POST("/users", userHandler::postUser)
        }
    }
}

There doesn't seem to be any relevant documentation about this.
How can I use @RouterOperation in coRouter DSL?

gumimin
  • 13
  • 1
  • 3

1 Answers1

3

The same principles applies to Kotlin DSL (coRouter): CoRouterFunctionDsl Beans are instances of RouterFunction.

Here is a sample syntax:

@FlowPreview
@Bean
@RouterOperations(
        RouterOperation(path = "/test", method = arrayOf(RequestMethod.GET), beanClass = ProductRepositoryCoroutines::class, beanMethod = "getAllProducts"),
        RouterOperation(path = "/test/{id}", method = arrayOf(RequestMethod.GET), beanClass = ProductRepositoryCoroutines::class, beanMethod = "getProductById"))
fun productRoutes(productsHandler: ProductsHandler) = coRouter {
    GET("/test", productsHandler::findAll)
    GET("/test/{id}", productsHandler::findOne)
}
brianbro
  • 4,141
  • 2
  • 24
  • 37
  • Thank you for replying. I know CoRouterFunctionDsl Beans are instances of RouterFunction and I can put @RouterOperation annotation on it. Apart from this, I want to put the annotation on route builder functions (such as GET, POST and so on) in coRouter DSL. If I split the routes into some RouterFunction beans, I can't commonize filters and error handling. So I want to write many paths in one coRouter using nest(). – gumimin Jul 04 '20 at 12:32
  • 1
    However, if I write the docs with RouterOperations annotation on one coRouter, the more paths you have, the larger the annotations become, and it's hard to understand which describe corresponds to which HandlerFunction. So I want to write docs with RouterOperation for each builder function per path method in coRouter. Is there anything way to solve it? – gumimin Jul 04 '20 at 12:35
  • You can, instead of using the bean and method class, you can use the swagger annotation @Operation. – brianbro Jul 05 '20 at 18:16
  • Thanks! I could put the Operation annotation on handler functions and shrink the RouterOperation annotation. – gumimin Jul 07 '20 at 11:27
  • @gumimin Is it possible for you to share an example – VenoM Jun 17 '21 at 14:50
  • @gumimin sorry for necroposting, but any example code would be very appreciated – Nikky Aug 11 '22 at 06:32