英文:
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()) {
//无法获取错误消息体 :'(
LOGGER.warn("第三方API响应状态码 {},错误消息体 {},请求有效载荷为 {}", clientResponse.statusCode().value(), myPojo, null);
//注意:我不想在这里抛出任何异常
}
//返回原始有效载荷mypojo对象
return Mono.just(myPojo);
}
我还尝试了onError、onStatus,但没有成功。
问题:
使用这个结构,我能够获取状态码,但不能获取错误消息体,同时返回了一个奇怪的Mono
如何在不抛出任何异常的情况下记录状态码和错误消息体,并能够返回原始请求有效载荷?
英文:
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<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 MyPojomyPojo) {
if (200 != clientResponse.statusCode().value()) {
//Not able to get the error message body :'(
LOGGER.warn("The third party API responded status code {} with error message body {} for this request payload {}", 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<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);
}
})
);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论