最近,我一直在使用WebClient。我正在创建同步请求和异步请求。在同步请求中,我能够捕获WebClientResponseException,而当我尝试在异步请求中捕获WebClientResponseException(同样使用通用异常,不起作用)时,无法将其捕获。我在互联网上找不到任何例子。我不确定是否可以在异步网络请求中进行此类异常处理。
以下是代码示例:
同步请求(可以捕获例外)
{
webClient
.post()
.uri(POST_ENDPOINT)
.bodyValue(cloudEvent)
.header("Authorization", token)
.retrieve()
.toEntity(String.class)
.block();
}
catch(final WebClientResponseException e)
{
log.error(String.valueOf(e));
throw new MyCustomException ("");
}
async请求(如果不能抓到例外)
try
{
stream=
webClient
.get()
.uri(GET_ENDPOINT)
.header("Authorization", token)
.retrieve()
.bodyToFlux(Any_Parametrized_Type_Reference);
}
catch(ffinal WebClientResponseException e)
{
log.error(String.valueOf(e));
throw new MyCustomException ("");
}
提前感谢您的任何响应。
Recently I have been working with WebClient. I am creating both a sync request and an async request. In a sync request, I am able to catch WebClientResponseException whereas when I try to catch WebClientResponseException (tried with generic Exception as well, didn't work) in an async request, it cannot be caught. I couldn't find any example on the internet. I am not sure if such exception handling is possible in async WebClient requests.
Here are the code examples:
Sync Request (Where the exception can be caught)
{
webClient
.post()
.uri(POST_ENDPOINT)
.bodyValue(cloudEvent)
.header("Authorization", token)
.retrieve()
.toEntity(String.class)
.block();
}
catch(final WebClientResponseException e)
{
log.error(String.valueOf(e));
throw new MyCustomException ("");
}
Async Request (Where the exception cannot be caught)
try
{
stream=
webClient
.get()
.uri(GET_ENDPOINT)
.header("Authorization", token)
.retrieve()
.bodyToFlux(Any_Parametrized_Type_Reference);
}
catch(ffinal WebClientResponseException e)
{
log.error(String.valueOf(e));
throw new MyCustomException ("");
}
Thanks in advance for any response.
发布评论
评论(2)
异步意味着该过程将转移到另一个线程。 BodyTofLux将执行此操作并完成执行,但是您对该线程没有任何控制权,因为在当前线程中,该功能已执行,并且不会触发问题。
这类似于JS中的承诺,如果有任何例外,您应该有回调。
BodyTofLux之后,您可以添加这些回调。
Onerrormap,OnerRreterturn和Doonerror会收到您的回调,并在例外时执行Lambda方法。
更多信息在这里: https://www.baeldung.com/spring-webflux--webflux--webflux---超时#异常处理
Async means that the process will be transferd to another thread. bodyToFlux will do this and finish the execution, but you don´t have any control over that thread because, in your current thread, the function was executed and it doesn't trigger an issue.
This is similar to Promises in JS, where you should have a callback in case of any exception.
after bodyToFlux, you can add those callbacks.
onErrorMap, onErrorReturn and doOnError receive your callbacks and will execute the lambda methods in case of exception.
More info here: https://www.baeldung.com/spring-webflux-timeout#exception-handling
感谢 @carloscárdenas;当我本地测试时,以下更改对我有用。
目前,我在相关的单元测试中有问题。它在应该时不会引发异常,因此我无法捕获异常。我认为我缺少测试异步流的一些东西。我的测试看起来像这样:
Thanks to @CarlosCárdenas; the following changes worked for me when I locally tested it.
Currently, I have an issue with the related unit test. It doesn't throw an exception when it should, so I cannot catch the exception. I think I'm missing something about testing asynchronous flow. My test looks like this: