Spring WebFlux - 如何捕获冲突异常

阿杰·库马尔

我有一个像下面这样的方法:

public String createFolder3(String folderName, String parentFolderId)
{
    String requestJson = "{\"name\": " + folderName + "}";
    return webClient.post()
        .uri("/the/uri/goeshere/" + parentFolderId + "/children")
        .body(Mono.just(requestJson), String.class)
        .retrieve()
        .onStatus(httpStatus -> HttpStatus.CONFLICT.equals(httpStatus),
                clientResponse -> Mono.error(new Exception("Some Conflict Occurred")))
        .bodyToMono(String.class).block();
}

但每次我得到以下(巨大的,为了简洁起见,我把它缩短了)例外。我不想在服务器端控制台上显示这个巨大的异常。我在这里做错了什么或错过了什么?

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'processJob': Invocation of init method failed; nested exception is reactor.core.Exceptions$ReactiveException: java.lang.Exception: Some Error Occurred
.....
Caused by: reactor.core.Exceptions$ReactiveException: java.lang.Exception: Some Error Occurred
.....
Error has been observed at the following site(s):
    |_ checkpoint ⇢ 409 from POST http:.....
....
00米

如果您希望在 409 的情况下返回响应正文,您可以捕获WebClientResponseException

.retrieve()
.bodyToMono(String.class)
.onErrorResume(
  WebClientResponseException.class,
  e -> {
    if (e.getStatusCode() == HttpStatus.CONFLICT) {
      // log
      return Mono.just(e.getResponseBodyAsString());
    }
    return Mono.error(e);
  }
)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章