Spring Webflux : Webclient : Get body on error

后端 未结 8 2240
失恋的感觉
失恋的感觉 2021-02-05 09:04

I am using the webclient from spring webflux, like this :

WebClient.create()
            .post()
            .uri(url)
            .syncBody(body)
            .a         


        
8条回答
  •  北恋
    北恋 (楼主)
    2021-02-05 09:27

    Don't we have onStatus()?

        public Mono cancel(SomeDTO requestDto) {
            return webClient.post().uri(SOME_URL)
                    .body(fromObject(requestDto))
                    .header("API_KEY", properties.getApiKey())
                    .retrieve()
                    .onStatus(HttpStatus::isError, response -> {
                        logTraceResponse(log, response);
                        return Mono.error(new IllegalStateException(
                                String.format("Failed! %s", requestDto.getCartId())
                        ));
                    })
                    .bodyToMono(Void.class)
                    .timeout(timeout);
        }
    

    And:

        public static void logTraceResponse(Logger log, ClientResponse response) {
            if (log.isTraceEnabled()) {
                log.trace("Response status: {}", response.statusCode());
                log.trace("Response headers: {}", response.headers().asHttpHeaders());
                response.bodyToMono(String.class)
                        .publishOn(Schedulers.elastic())
                        .subscribe(body -> log.trace("Response body: {}", body));
            }
        }
    

提交回复
热议问题