Using Mono<T> in Spring to populate a Cloud Firestore entity

326 Views Asked by At

I am using spring-cloud-gcp-starter-data-firestore for accessing Google Cloud Firestore in my Java Spring application.

Currently, my entity looks like this:

public class Subscription {

    public String userId;
    public String companyId;

    // other properties

}

However, I obtain the userId and companyId via a reactor.core.publisher.Mono in org.springframework.security.core.context.ReactiveSecurityContextHolder.

How can I persist both properties which are nested inside Monos without resorting to Mono#block?

2

There are 2 best solutions below

0
On BEST ANSWER

I am now using Mono#zipWith to combine both Monos. Then I am creating the entity inside Mono#flatMap.

service.getCompanyId()
   .zipWith(service.getUserId())
   .flatMap(objects -> createEntity(objects.getT1(), objects.getT2()))
0
On

I suggest following the tutorial in the Google codelab.

Here you can find that Firestore can be in Datastore mode which makes the previous tutorial suitable for you.