Spring Boot 2.0 WebClient在继续之前处理404

时间:2018-05-07 12:39:10

标签: spring-boot kotlin spring-webflux

我想在继续管道之前从API调用处理404。如果传入的customerId没有带回记录,我想抛出404,我已经尝试在第一个flatmap中检查stauscode但是下面的map需要Mono以便不编译。

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

1 个答案:

答案 0 :(得分:0)

啊哈哈在打了几个小时后的那一刻:

Map<String,Object> map = new HashMap<>();

Map<String,String> stringifiedMap = map.entrySet().stream()
             .filter(m -> m.getKey() != null && m.getValue() !=null)
             .collect(Collectors.toMap(Map.Entry::getKey, e -> (String)e.getValue()));