Spring WebFlux WebClient处理ConnectTimeoutException

丹尼斯·尼森

我正在使用Spring WebFlux Webclient进行REST调用。我已将连接超时配置为3000毫秒,因此:

WebClient webClient = WebClient.builder()
    .clientConnector(new ReactorClientHttpConnector(options -> options
        .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000)))
    .build();

return webClient.get()
    .uri("http://localhost:8081/resource")
    .retrieve()
    .onStatus(HttpStatus::isError, clientResponse -> {
        // Some logging..
        return Mono.empty();
    })
    .bodyToMono(MyPojo.class);

onStatus方法Mono为每个400/500响应代码返回一个空如何为连接超时甚至读取/写入超时执行相同的操作。现在,它只是抛出一个io.netty.channel.ConnectTimeoutException不由onStatus

我不需要@ExceptionHandler在控制器上使用,因为这些REST调用是更复杂的流程的一部分,并且通过空Mono将元素忽略即可。

早在spring-webRestTemplate,我记得连接超时也导致RestClientException因此,我们可以捕获RestClientException所有异常和超时。我们有没有办法做到这一点WebClient

布莱恩·克洛泽尔

ReactoronError***为此提供了多个运算符:

return webClient.get()
    .uri("http://localhost:8081/resource")
    .retrieve()
    .onErrorResume(ex -> Mono.empty())
    .onStatus(HttpStatus::isError, clientResponse -> {
        // Some logging..
        return Mono.empty();
    })
    .bodyToMono(MyPojo.class);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring WebFlux WebClient-如何解决400错误请求

在测试期间在Spring Webflux中进行全局异常处理

在Spring Webflux中处理条件响应的正确方法是什么

处理Spring Webflux MultipartFile.transferTo中的错误

Spring Webflux WebClient将文件发布到客户端

Spring WebFlux WebClient:延迟执行

使用WebClient Spring WebFlux的多个请求

Spring Integration中的WebFlux出站网关错误处理

Spring Integration WebFlux错误处理

使用spring-webflux WebClient测试虚拟时间的问题

How to set a timeout in Spring 5 WebFlux WebClient

Spring Security Webflux /反应式异常处理

Spring WebFlux WebClient挂起,Mono.timeout无法捕获它

Spring-Integration Webflux异常处理

对于Spring WebFlux的WebClient,如何捕获Netty的异常(例如ProxyConnectException)

Spring Boot 2.1.5,WebFlux,Reactor:如何正确处理MDC

配置Spring WebFlux WebClient使用自定义线程池

使用Spring Boot + WebFlux进行全局错误处理

在Spring WebFlux中处理全球方案

在高负载下测试spring webflux Webclient的问题

Spring Webflux-使用Webflux WebClient时不显示日志记录连接ID和新的连接日志

Spring WebFlux-为什么我必须等待WebClient响应?

Spring @Async与Spring WebFlux

Spring WebFlux 和 WebClient 更改错误响应

Spring Webflux - 基于 TLSv1.2 的 WebClient

如何处理 Reactive Spring webflux 中的错误

Spring webflux 中的异常处理

如何在 Spring Webflux WebClient 中处理令牌刷新

Java Spring Webflux - WebClient .onStatus((HttpStatus::isError) 返回错误处理