Spring Webflux : Webclient : Get body on error

后端 未结 8 2236
失恋的感觉
失恋的感觉 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:25

    I do something like this:

    Mono responseMono = requestSpec.exchange()
                .doOnNext(response -> {
                    HttpStatus httpStatus = response.statusCode();
                    if (httpStatus.is4xxClientError() || httpStatus.is5xxServerError()) {
                        throw new WebClientException(
                                "ClientResponse has erroneous status code: " + httpStatus.value() +
                                        " " + httpStatus.getReasonPhrase());
                    }
                });
    

    and then:

    responseMono.subscribe(v -> { }, ex -> processError(ex));
    

提交回复
热议问题