【问题标题】:Spring WebClient odd behaviourSpring WebClient 奇怪的行为
【发布时间】:2021-07-28 14:55:39
【问题描述】:

我正在使用webClient使用以下代码执行http请求

  @Bean
    public CommandLineRunner commandLineRunner() {
        return args -> Flux.just(1)
            .flatMap(__ -> WebClient
                .builder()
                .build()
                .post()
                .uri("http://thisuridoesnotexist/")
                .exchangeToMono(response -> Mono.just(1))
                .doOnError(throwable -> System.out.println("1"))
                .onErrorResume(
                    Exception.class,
                    throwable -> {
                        System.out.println("2");
                        return Mono.error(new RuntimeException("This vanishes"));
                    })
                .onErrorContinue((throwable, o) -> {
                    System.out.println("8");
                }))
            .doOnError(__ -> System.out.println("9"))
            .onErrorContinue((throwable, obj) -> System.out.println("10"))
            .subscribe();
    }

webClient 尝试与未应答的 uri 对话并引发连接被拒绝异常时,实际会发生此问题。正如您在代码中看到的那样,有多个 Xerror 运算符(当然,它们只是为 PoC 放置的)但这里唯一真正起作用的是 onErrorContinue8。预计910 不会工作,因为“错误”已在8 上解决,但是!如果我注释掉8,那么它会跳过9 并直接转到10。最后,无论如何,它总是会跳过 12 运算符。

阅读reactor 的文档和doOnError 上面的javadoc 可以说,它清楚地指出,对于Xerror 运算符,它包含any 此处不会发生的异常。

最后但并非最不重要的一点是删除 onErrorContinue 129 按预期工作。因此,如果 onErrorContinue 是一个急切地消耗所有东西的问题,我该如何使用“catchAll”故障保护,以防在特定情况下无法“预测”错误或者只是处理错误?

只是为了完成这个演示项目中使用的 pom 代码

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

    标签: java spring webclient reactor


    【解决方案1】:

    根据文档描述,这不是 Spring 的行为,而是 onErrorContinue 的:

    https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html#onErrorContinue-java.util.function.BiConsumer-

    让兼容的运算符上游通过从序列中删除有罪的元素并继续处理后续元素来从错误中恢复。通过提供的 BiConsumer 通知恢复的错误和相关值。或者,从该双消费者抛出的异常会将抛出的异常传播到下游,而不是原始错误,该异常会作为抑制异常添加到新错误中。

    ...

    请注意,onErrorContinue() 是一个专业的运算符,可以使您的反应链的行为不清楚。 它在上游而不是下游运算符上运行,它需要特定的运算符支持才能工作,并且范围可以轻松地向上游传播到没有预料到的库代码中(导致意外行为。)

    所以 doOnError 没有收到异常,因为上游已经恢复。

    为了了解它的真正含义以及如何处理它,我做了一些实验并发现,如果你将throw new RuntimeException()8 放在块中,那么12 会被捕获.但不是9,因为10 的行为相同

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      • 2011-08-09
      • 1970-01-01
      相关资源
      最近更新 更多