flatMap() method of Mono/Flux object in Project Reactor

Unlike the flatMap() method in the Java Stream object or the flatMap() method in the Optional object in Java, the flatMap() method of the Mono object or the Flux object in the Project Reactor is capable of converting the data which emitted by the Mono or Flux object to another data type and that other data will be emitted by another Mono or Flux object.

For example, I have a simple Mono object as follows:

Using the flatMap() method, we can convert the data of the Mono object from the String data type to the Integer data type, and then emit it with another Mono object. For example:

In the code above, from the length of the data in the mono1 object, I took the length of that data and used a different Mono object to generate this new data.

Result:

flatMap() method of Mono/Flux object in Project Reactor

Flux’s flatMap() method is similar.

Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment