记录Spring Webflux Webclient的错误状态码和错误状态消息。

huangapple go评论68阅读模式
英文:

Log both error status code and error status message with Spring Webflux Webclient

问题

以下是翻译好的部分:

我想要实现的目标:

发送带有MyPojo请求体的HTTP请求到外部API,并返回原始的MyPojo。但是当出现错误(外部API未返回200)时,记录错误代码和错误消息体(不要抛出任何异常!)并仍然返回原始的MyPojo。

我尝试过的方法:

public Flux<MyPojo> question(Flux<MyPojo> myPojoFlux) {
    return myPojoFlux.flatMap(myPojo -> webClient.post().bodyValue(myPojo).exchangeToMono(clientResponse -> logStatusCodeAndErrorBodyWhenNotStatusOk(clientResponse, myPojo)));
}

private Mono<MyPojo> logStatusCodeAndErrorBodyWhenNotStatusOk(final ClientResponse clientResponse, final MyPojo myPojo) {
    if (200 != clientResponse.statusCode().value()) {
        //无法获取错误消息体 :&#39;(
        LOGGER.warn("第三方API响应状态码 {},错误消息体 {},请求有效载荷为 {}", clientResponse.statusCode().value(), myPojo, null);
        //注意:我不想在这里抛出任何异常
    }
    //返回原始有效载荷mypojo对象
    return Mono.just(myPojo);
}

我还尝试了onError、onStatus,但没有成功。

问题:

使用这个结构,我能够获取状态码,但不能获取错误消息体,同时返回了一个奇怪的Mono对象,使用了Mono.just(myPojo);

如何在不抛出任何异常的情况下记录状态码和错误消息体,并能够返回原始请求有效载荷?

英文:

What I would like to achieve:

Send http requests with a body MyPojo to an external API and return the original MyPojo.
But when there is an error (external API not returning 200) log the error code and the error message body (without throwing anything!) and still return the original MyPojo.

What did I try:

    public Flux&lt;MyPojo&gt; question (Flux&lt;MyPojo&gt; myPojoFlux) {
        return myPojoFlux.flatMap(myPojo -&gt; webClient.post().bodyValue(myPojo).exchangeToMono(clientResponse -&gt; logStatusCodeAndErrorBodyWhenNotStatusOk(clientResponse, myPojo)));
    }

    private Mono&lt;MyPojo&gt; logStatusCodeAndErrorBodyWhenNotStatusOk(final ClientResponse clientResponse, final MyPojomyPojo) {
        if (200 != clientResponse.statusCode().value()) {
            //Not able to get the error message body :&#39;(
            LOGGER.warn(&quot;The third party API responded status code {} with error message body {} for this request payload {}&quot;, clientResponse.statusCode().value(), myPojo, null);
            //Note: I do not want to throw any exception here
        }
//return the original payload mypojo object
        return Mono.just(myPojo);
    }

I also tried onError, onStatus, no luck

Question :

With this construct, I am able to get the status code, but not the error message body, while returning a strange Mono<MyPojo> with Mono.just(myPojo);

How to log both the status code and the error message body without (throwing anything) and being able to return the original request payload?

答案1

得分: 0

这可能是您正在寻找的内容:

public Flux<MyPojo> question(Flux<MyPojo> myPojoFlux) {
    return myPojoFlux.flatMap(myPojo -> webClient.post()
        .bodyValue(myPojo)
        .exchangeToMono(clientResponse -> {
            if (clientResponse.statusCode().is2xxSuccessful()) {
                return clientResponse
                    .releaseBody() // be a good citizen and consume the body
                    .thenReturn(myPojo);
            } else {
                return clientResponse
                    .bodyToMono(String.class) // consume the body to construct response
                    .doOnNext(response -> LOG.warn("Error {} occurred. HTTP status code was {}", response, clientResponse.statusCode()))
                    .thenReturn(myPojo);
            }
        })
    );
}

如果您需要进一步的帮助,请告诉我。

英文:

This is probably what you are looking for:

public Flux&lt;MyPojo&gt; question(Flux&lt;MyPojo&gt; myPojoFlux) {
    return myPojoFlux.flatMap(myPojo -&gt; webClient.post()
        .bodyValue(myPojo)
        .exchangeToMono(clientResponse -&gt; {
            if (clientResponse.statusCode().is2xxSuccessful()) {
                return clientResponse
                    .releaseBody() // be a good citizen and consume the body
                    .thenReturn(myPojo);
            } else {
                return clientResponse
                    .bodyToMono(String.class) // consume the body to construct response
                    .doOnNext(response -&gt; LOG.warn(&quot;Error {} occurred. HTTP status code was {}&quot;, response, clientResponse.statusCode()))
                    .thenReturn(myPojo);
            }
        })
    );
}

huangapple
  • 本文由 发表于 2023年5月10日 18:28:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217337.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定