I am trying to access a json using JsonPath
from a DocumentContext
in Reactive spring,
I then compare it with 'another string' to send a Mono<Boolean>
value based on the comparison.
However, I want the Mono.just
to log the error and return an empty String if the path
does not exist so that I compare it with the other string to return a Mono.just(false)
.
I tried multiple error handling methods and nothing seemed to execute the log method, nor it is returning an empty string to proceed with the next step i.e. comparison.
My method looks something like this:
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));
}
I am new to Spring Reactive, I may be missing something fundamental in this concept.
Kindly help me refactor the code to achieve the above goal, or point me to some blog / article where I can get the correct way to handle exception in this case.
Thanks in advance.
Try using Mono.fromCallable(()->student.read(“$.studentId” String.class))
or Mono.defer(() -> Mono.just(..))
instead of Mono.just()
. The issue is related to the fact how Mono.just(..)
is evaluated. you can look here for details.