Spring boot 继续之前,Spring Boot 2.0 WebClient句柄404
在继续管道之前,我想处理来自API调用的404。如果传入的customerId没有返回记录,我想抛出一个404,我已经尝试在第一个flatmap中检查StatusCode,但是下面的map需要一个Mono,因此无法编译Spring boot 继续之前,Spring Boot 2.0 WebClient句柄404,spring-boot,kotlin,spring-webflux,Spring Boot,Kotlin,Spring Webflux,在继续管道之前,我想处理来自API调用的404。如果传入的customerId没有返回记录,我想抛出一个404,我已经尝试在第一个flatmap中检查StatusCode,但是下面的map需要一个Mono,因此无法编译 @PostMapping(path = ["/customers/{customerId}/place"]) fun create(@PathVariable customerId: String): Mono<ResponseEntity<OrderP
@PostMapping(path = ["/customers/{customerId}/place"])
fun create(@PathVariable customerId: String): Mono<ResponseEntity<OrderPlacedResponse>> {
return webClient
.get()
.uri("/$customerId/cart", customerId)
.exchange()
.flatMap { response ->
response.bodyToMono(Cart::class.java)
}
.map { it.items.map { OrderItem(it.productId, it.quantity, it.price) } }
.map { items -> Order(customerId, items, UUID.randomUUID().toString()) }
.flatMap { orderRepository.save(it) }
.map {
ResponseEntity.ok(OrderPlacedResponse("Order Placed", it))
}
.doOnError {
ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.build<OrderPlacedResponse>().toMono()
}
}
啊哈,战斗数小时后的瞬间:
@PostMapping(path = ["/customers/{customerId}/place"])
fun create(@PathVariable customerId: String): Mono<ResponseEntity<OrderPlacedResponse>> {
return webClient
.get()
.uri("/$customerId/cart", customerId)
.exchange()
.flatMap { response ->
response.bodyToMono(Cart::class.java)
}
.map { it.items.map { OrderItem(it.productId, it.quantity, it.price) } }
.map { items -> Order(customerId, items, UUID.randomUUID().toString()) }
.flatMap { orderRepository.save(it) }
.map {
ResponseEntity.ok(OrderPlacedResponse("Order Placed", it))
}
.switchIfEmpty(
ResponseEntity
.status(HttpStatus.NOT_FOUND)
.build<OrderPlacedResponse>().toMono()
)
.doOnError {
ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.build<OrderPlacedResponse>().toMono()
}
}