1

Every time I think I understand Webflux and project reactor, I find out I have no idea. So I making some API calls... I want to call 1 first ... Get information back use that information, to make subsequent calls.

so I do this like so

public Mono<ResponseObject> createAggregatedRecords(RecordToPersist recordToPersist){
return randomApiClient.createRecord(recordToPersist)
    .flatMap(result -> {
        return Mono.zip(
                webClientInstance.createOtherRecord1(result.getChildRecord1()),
                webClientInstance2.createOtherRecord2(result.getChildRecord2()),
                webClientInstance3.createOtherRecord3(result.getChildRecord3()))
                .map(tupple -> {
                        ResponseObject respObj = new ResponseObject();
                        respObj.setChildResult1(tupple.getT1());
                        respObj.setChildResult2(tupple.getT2());
                        respObj.setChildResult3(tupple.getT3());
                        return respObj;
                }
    }).doOnSuccess(res -> log.info("This throws an error: {}", res.getChildResult1.getFirstField()))
}

Now, for some reason, I am returning a null object with this very code to my Controller and I am not printing out the object in Json.

I suspect it is because I am nesting the Mono.zip inside the flatmap, and am not returning the results back correctly. I am making all of those API calls though as my End-to-End integration tests are succeeding.

Now I thought that I would return that response object from the .map function from the Mono.zip chain and then return that to the flatMap call in the chain. If I put observers on the chain like a doOnSuccess and print out response object fields I get a null pointer ... Not sure what I am missing

  1. Is this a good pattern to achieve that goal? Or should I try a different path?
  2. Why can I not get the response Object to return?
SoftwareSavant
  • 9,467
  • 27
  • 121
  • 195
  • 1
    Something likely returns empty Mono in your code which then completes the whole chain as empty. Try to put doOnSuccess on each individual Mono to find the culprit. – Martin Tarjányi Jul 03 '21 at 10:30

0 Answers0