【问题标题】:Retrieve path variable on Spring Boot WebFlux (functional approach)在 Spring Boot WebFlux 上检索路径变量(功能方法)
【发布时间】:2019-01-06 06:00:15
【问题描述】:

假设我有这个路由器定义:

@Component
class PersonRouter(private val handler: PersonHandler) {
  @Bean
  fun router(): RouterFunction<ServerResponse> = router {
    ("/api/people" and accept(MediaType.APPLICATION_JSON_UTF8)).nest {
      GET("/{id}") { handler.findById(it) }
    }
  }

然后是这个处理程序:

@Component
@Transactional
class PersonHandler(private val repository: PersonRepository) {
  private companion object : KLogging()

  @Transactional(readOnly = true)
  fun findById(req: ServerRequest): Mono<ServerResponse> {
    logger.info { "${req.method()} ${req.path()}" }
    val uuid = ? // req.pathContainer().elements().last().value()
    return ServerResponse.ok()
        .contentType(MediaType.APPLICATION_JSON_UTF8)
        .body(BodyInserters.fromObject(repository.findById(uuid)))
        .switchIfEmpty(ServerResponse.notFound().build())
  }
}

我如何从ServerRequest 访问标识符(典型的@RestController 上的@PathVariable id: String)而不使用正则表达式、繁重的字符串等操作?

【问题讨论】:

  • 我不知道你想要实现什么,但 WebFlux 目前不支持事务注释。

标签: spring-boot kotlin spring-webflux


【解决方案1】:

啊!找到了!

通过这样做:req.pathVariable("id")

它一直都在那里......在官方Spring Framework(Web Reactive)文档中!

【讨论】:

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