![](/img/trans.png)
[英]How to use Mono and Flux in handler functions with Spring WebFlux Reactive ways
[英]Exceptions thrown by Mono inside flatmap not being handled by Spring webflux reactive stream
我正在使用 Spring webflux 开发反应流应用程序。 我有一个用例,我在其中执行 webclient 请求以获取一些数据,一旦获得响应数据,我就会对其进行验证,如果验证失败,我想抛出一个应该由
main<\/code>反应管道处理的异常。
我在平面图中使用 webclient 调用来使用管道的下一个运算符中的值。 我有类似于以下代码的内容:
public class Example {
public String getData(String name) {
return Mono.just(name)
.map(name -> name.toLowerCase())
.flatMap(name ->
// Webclient Request that returns a
// Mono<String> for example
.doOnSuccess(Validator::validateData); // The webclient request is chained to this doOnSuccess
)
.doOnError(ex -> log.error("Got an error, {}", er))
.onErrorMap(ex -> new AnotherCustomException(ex.getMessage()));
}
}
public class Validator {
public void validateData(String data) {
if(data.length() < 5) throw new CustomException("Invalid data received."); // public CustomException extends RuntimeException {...}
}
}
这就是你想要的
SomeWebclientCall().flatMap(value -> {
Validate.validate(value);
return Mono.just(value);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.