【问题标题】:Spring WebFlux functional router + OpenAPI UISpring WebFlux 功能路由器 + OpenAPI UI
【发布时间】:2021-07-29 20:11:32
【问题描述】:

我想在 Kotlin 上使用 Spring WebFlux 功能路由器创建 OpenAPI UI 规范。 Spring Boot v.2.5.2,springdoc-openapi v.1.5.9。 这是我的路由器类:

@Configuration
class UserServiceRouter {

    @Bean
    @RouterOperations(
        RouterOperation(
            path = "/user/{id}",
            method = [RequestMethod.GET],
            beanClass = UserService::class,
            beanMethod = "getUserCredentialsById",
        operation = Operation(
            parameters = [Parameter(
                style = ParameterStyle.SIMPLE,
                explode = Explode.FALSE,
                `in` = ParameterIn.PATH
            )]
        )
        ),
        RouterOperation(
            path = "/user/profile/{id}",
            method = [RequestMethod.GET],
            beanClass = UserService::class,
            beanMethod = "getUserProfileById",
        operation = Operation(
            parameters = [Parameter(
                style = ParameterStyle.SIMPLE,
                explode = Explode.FALSE,
                `in` = ParameterIn.PATH
            )]
        )
    )
    fun userRoute(userServiceHandler: UserServiceHandler) = coRouter {
        ("/user").nest {
            GET("/{id}", userServiceHandler::getUserCredentialsById)
            GET("/profile/{id}", userServiceHandler::getUserById)
        }
    }
}

我想创建一个类似的 GET 查询

http://localhost:8080/user/1

其中 1 是我想要的用户 ID。

但我的 OpenAPI 的 UI 仍然尝试使用 GET 参数创建查询

http://localhost:8080/user/{id}?id=1

这显然会导致错误 400。 很高兴知道应该如何正确配置我的@RouterOperations。

【问题讨论】:

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


    【解决方案1】:

    路径变量的正确配置如下所示 (为简洁起见,我在这里只留下了一个 GET 查询):

    @Bean
    @RouterOperations(
        RouterOperation(
            path = "/user/{id}",
            method = arrayOf(RequestMethod.GET),
            beanClass = UserService::class,
            beanMethod = "getUserCredentialsById",
            operation = Operation(
                operationId =  "getUserCredentialsById",
                method = "GET",
                parameters = [
                    Parameter(
                        name = "id",
                        `in` = ParameterIn.PATH,
                        style = ParameterStyle.SIMPLE,
                        explode = Explode.FALSE,
                        required = true
                    )]
            )
        )
    )
    fun userRoute(userServiceHandler: UserServiceHandler) = coRouter {
        path("/user")
            .nest {
                GET("/{id}", userServiceHandler::getUserCredentialsById)
            }
    }
    

    请参阅规格here。 重点:

    • 操作 ID 为必填项
    • Operationparameters[] 中的默认值是“查询参数”而不是“路径参数”+ 我们应该 显式指向参数名称 (name = "id") 以覆盖默认值 行为。
    • 如上所述,ParameterIn.PATH 也是强制性的。

    【讨论】:

      猜你喜欢
      • 2018-09-22
      • 2021-11-08
      • 1970-01-01
      • 2020-08-12
      • 1970-01-01
      • 2016-01-06
      • 2018-08-22
      • 2016-07-04
      • 2019-01-06
      相关资源
      最近更新 更多