Spring reactive-如何处理Mono.just抛出的运行时异常



我正试图使用JsonPath从Reactive spring中的DocumentContext访问json,

然后,我将其与"另一个字符串"进行比较,以根据比较发送Mono<Boolean>值。

但是,如果path不存在,我希望Mono.just记录错误并返回一个空字符串,以便将其与其他字符串进行比较以返回Mono.just(false)

我尝试了多种错误处理方法,但似乎没有执行日志方法,也没有返回一个空字符串来继续下一步,即比较。

我的方法看起来像这样:

private Mono<Boolean> isMatching(DocumentContext student) {
return Mono.just(
student.read(“$.studentId” String.class)) ///*throws exception as studentId is not present*/
.switchIfEmpty(Mono.just(EMPTY_STRING))
.doOnError(error -> {
log.error("error occurred {}", error.getMessage());
}).onErrorReturn("") ///*want to send empty string for comparison*/
.map(
studentId ->
“S101”.equals(studentId));
}

我是Spring Reactive的新手,我可能在这个概念中遗漏了一些基本的东西。

请帮助我重构代码以实现上述目标,或者向我介绍一些博客/文章,在这里我可以获得处理这种情况下异常的正确方法。

提前谢谢。

尝试使用Mono.fromCallable(()->student.read(“$.studentId” String.class))Mono.defer(() -> Mono.just(..))而不是Mono.just()。这个问题与如何评估Mono.just(..)有关。你可以在这里查看详细信息。

最新更新