Java 来自subscribe的WebClient返回值
我尝试使用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
.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应用程序”也支持反应式返回类型,请参见此处:(表中倒数第二行)。我没有在任何地方提到它不支持反应式返回类型。我写道,你可以……你应该。。。。