【发布时间】: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