Spring Webflux : Webclient : Get body on error

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

    I got the error body by doing like this:

    webClient
    ...
    .retrieve()    
    .onStatus(HttpStatus::isError, response -> response.bodyToMono(String.class) // error body as String or other class
                                                       .flatMap(error -> Mono.error(new RuntimeException(error)))) // throw a functional exception
    .bodyToMono(MyResponseType.class)
    .block();
    

提交回复
热议问题