繁体   English   中英

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原因,因此只能在它周围添加一个try catch。 我读了几篇文章,这是我最后一次添加Mono.error(throwable)的尝试,但是由于没有回报,我很确定这是没有效果的原因。

这是一个POST调用,成功返回204 No Content。 目前我得到的是422,尽管在这个特定问题上这并不重要。

有人可以教我如何将异常返回给调用环境吗?

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

您的代码应如下所示

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM