Spring Webflux : Webclient : Get body on error

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

    You could also do this

    return webClient.getWebClient()
     .post()
     .uri("/api/Card")
     .body(BodyInserters.fromObject(cardObject))
     .exchange()
     .flatMap(clientResponse -> {
         if (clientResponse.statusCode().is5xxServerError()) {
            clientResponse.body((clientHttpResponse, context) -> {
               return clientHttpResponse.getBody();
            });
         return clientResponse.bodyToMono(String.class);
       }
       else
         return clientResponse.bodyToMono(String.class);
    });
    

    Read this article for more examples link, I found it to be helpful when I experienced a similar problem with error handling

提交回复
热议问题