Aqu*_*lia 2 java spring-boot spring-webclient
我尝试使用 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);
}
});
Run Code Online (Sandbox Code Playgroud)
首先,您是在编写Webflux应用程序还是只是WebClient在 Spring Web 应用程序中使用而不是在RestTemplate.
如果WebClient在常规 Spring Web 应用程序中使用,您只需调用Block()阻塞WebClient线程来获取数据,直到收到数据,然后为您返回数据。
WebClient如果在应用程序中使用,Webflux您应该将Mono或Flux全部返回给调用客户端,因为调用客户端就是订阅者。客户端(例如 React 应用程序、Angular 应用程序或其他应用程序)订阅您的 Spring 应用程序。您的 Spring 应用程序反过来订阅其他内容,然后将数据转发给原始订阅者。
您还没有说明您正在编写什么样的应用程序,而且您的代码也很模糊,这反过来意味着答案也是模糊的。
| 归档时间: |
|
| 查看次数: |
10579 次 |
| 最近记录: |