Spring 5 Webclient 在 doAfterSuccessOrError 中抛出异常

Spring 5 Webclient throw exception in doAfterSuccessOrError

我是一名 java 7 开发人员(终于)在 java 8 中迈出第一步。其中很多东西对我来说仍然是新的。我正在尝试使用 spring 5 WebClient,因为文档指出 RestTemplate 将被移走以支持 WebClient。

        webClient
                .method(HttpMethod.POST)
                .uri(uriBuilder -> uriBuilder.pathSegment("api", "payments").build())
                .body(BodyInserters.fromObject(createPostRequest(paymentConfirmationData)))
                .accept(MediaType.APPLICATION_JSON)
                .exchange()
                .doAfterSuccessOrError((clientResponse, throwable) -> {
                    if (clientResponse.statusCode().is5xxServerError()
                        || clientResponse.statusCode().is4xxClientError()) {
                         logger.error("POST request naar orchestration layer mislukt, status: [{}]", clientResponse.bodyToMono(String.class));
                         Mono.error(throwable);
                    } else {
                        logger.error("POST request naar orchestration layer gelukt");
                    }
                })
                .block();

我试图在 .doAfterSuccesOrError 中抛出异常。但是我不能使用 throw throwable cause 然后它只是在它周围添加了一个 try catch 。我读了几篇文章,这是我最后一次尝试添加 Mono.error(throwable) 但是因为没有 return 我很确定这就是没有效果的原因。

这是一个 POST 调用,成功时 return 返回 204 无内容。目前我得到了 422,尽管这在这个特定问题中不应该很重要。

谁能教我如何将异常抛回给调用环境?

有一种处理状态代码的特殊方法。更多here

您的代码应该如下所示

webClient.method(HttpMethod.POST)
         .uri(uriBuilder -> uriBuilder.pathSegment("api", "payments").build())
         .body(BodyInserters.fromObject(createPostRequest(paymentConfirmationData)))
         .accept(MediaType.APPLICATION_JSON)
         .retrieve()
         .onStatus(HttpStatus::is4xxServerError, response -> ...)
         .onStatus(HttpStatus::is5xxServerError, response -> ...)
         ...
         .block();

请记住,当使用 onStatus 时,如果预期响应有内容,那么 onStatus 回调应该使用它。如果没有,内容将被自动排空以确保资源被释放。

我最终得到了以下代码

webClient
        .method(HttpMethod.POST)
        .uri(uriBuilder -> uriBuilder.pathSegment("api", "payments").build())
        .body(BodyInserters.fromObject(createPostRequest(paymentConfirmationData)))
        .accept(MediaType.APPLICATION_JSON)
        .exchange()
        .doOnSuccess((clientResponse) -> {
            if (clientResponse.statusCode().is5xxServerError()
                    || clientResponse.statusCode().is4xxClientError()) {
                logger.error("POST request naar orchestration layer mislukt, status: [{}]", clientResponse.statusCode());
                throw new RuntimeException("POST request naar orchestration layer mislukt");
            } else {
                logger.error("POST request naar orchestration layer gelukt");
            }
        })
        .doOnError((throwable) -> {
            logger.error("POST request naar orchestration layer mislukt");
            throw new RuntimeException("POST request naar orchestration layer mislukt", throwable);
        })
        .block();

对于那些搜索如何处理异常和错误的人。查看 Reactor 项目的参考文档:https://projectreactor.io/docs/core/release/reference/index.html#_error_handling_operators