Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 来自subscribe的WebClient返回值_Java_Spring Boot_Spring Webclient - Fatal编程技术网

Java 来自subscribe的WebClient返回值

Java 来自subscribe的WebClient返回值,java,spring-boot,spring-webclient,Java,Spring Boot,Spring Webclient,我尝试使用webclient非阻塞方法验证验证码响应。所以 它可以工作,但我需要我的方法返回布尔值,而不是异常。如何从订阅中返回值 webClient .post() .uri(verifyUri) .exchange() .flatMap(res -> res.bodyToMono(GoogleResponse.class))

我尝试使用webclient非阻塞方法验证验证码响应。所以 它可以工作,但我需要我的方法返回布尔值,而不是异常。如何从订阅中返回值

        webClient
                .post()
                .uri(verifyUri)
                .exchange()
                .flatMap(res -> res.bodyToMono(GoogleResponse.class))
                .doOnError(e -> log.error("Request error"))
                .subscribe(GoogleResponse -> {
                    try {
                        log.debug("Google response: {}", GoogleResponse);
                        if (!GoogleResponse.isSuccess()) {
                            throw new ReCaptchaInvalidException("reCaptcha response is not valid");
                        }
                    } catch (ReCaptchaInvalidException e) {
                        throw new ReCaptchaUnavailableException("Registration unavailable at this time.  Please try again later.", e);
                    }
                });

首先,您是在编写
Webflux
应用程序,还是在SpringWeb应用程序中使用
WebClient
而不是
RestTemplate

如果在常规Spring web应用程序中使用
WebClient
,您只需为
WebClient
调用
Block()
,从线程中提取数据块,直到收到数据,然后为您返回数据

如果在
Webflux
应用程序中使用
WebClient
,则应将
Mono
Flux
一直返回到主叫客户端,因为主叫客户端是订户。客户端(比如react应用程序、angular应用程序或其他什么)订阅您的spring应用程序。您的spring应用程序反过来订阅其他内容,然后将数据转发给原始订户


您没有说明您正在编写什么类型的应用程序,代码也不清楚,这意味着答案也不清楚。

我必须在我的springboot应用程序中使用WebClient,因为resttemplate已被弃用。实际上,“常规Spring Web应用程序”也支持反应式返回类型,请参见此处:(表中倒数第二行)。我没有在任何地方提到它不支持反应式返回类型。我写道,你可以……你应该。。。。