【问题标题】:springdoc-openapi: How can I use @RouterOperation in coRouter DSLspringdoc-openapi:如何在 coRouter DSL 中使用 @RouterOperation
【发布时间】:2020-10-22 22:50:16
【问题描述】:

我将 springdoc-openapi 与 Kotlin 和 WebFlux.fn 一起使用。
我想在 CoRouterFunctionDsl 中的每个路径上使用 @RouterOperation 注释,但我不能。

@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)
        }
    }
}

似乎没有任何相关文档。
如何在 coRouter DSL 中使用 @RouterOperation?

【问题讨论】:

    标签: kotlin spring-webflux springdoc springdoc-openapi-ui


    【解决方案1】:

    同样的原则也适用于 Kotlin DSL(coRouter):CoRouterFunctionDsl Bean 是 RouterFunction 的实例。

    这是一个示例语法:

    @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)
    }
    

    【讨论】:

    • 感谢您的回复。我知道 CoRouterFunctionDsl Bean 是 RouterFunction 的实例,我可以在其上添加 @RouterOperation 注释。除此之外,我想在 coRouter DSL 中将注解放在路由构建器函数(如 GET、POST 等)上。如果我将路由拆分成一些 RouterFunction bean,我就不能通用过滤器和错误处理。所以我想用nest()在一个coRouter中写很多路径。
    • 但是,如果我在一个coRouter上写带有RouterOperations注解的文档,你拥有的路径越多,注解就越大,很难理解哪个描述对应哪个HandlerFunction。所以我想为coRouter中每个路径方法的每个构建器函数编写带有RouterOperation的文档。有什么办法可以解决吗?
    • 可以不使用bean和方法类,而是使用swagger注解@Operation。
    • 谢谢!我可以将 Operation 注释放在处理函数上并缩小 RouterOperation 注释。
    • @gumimin 可以分享一个例子吗
    猜你喜欢
    • 2020-08-30
    • 1970-01-01
    • 2020-06-28
    • 2020-10-02
    • 2021-11-14
    • 2020-12-07
    • 2022-09-28
    • 2020-11-18
    • 2020-09-17
    相关资源
    最近更新 更多