How to get String from Mono<String> in reactive java

iterate mono<list
flux to list
flux backpressure example
flux stream
kotlin unwrap mono
mono delay

I have a method which accepts Mono as a param. All I want is to get the actual String from it. Googled but didn't find answer except calling block() over Mono object but it will make a blocking call so want to avoid using block(). Please suggest other way if possible. The reason why I need this String is because inside this method I need to call another method say print() with the actual String value. I understand this is easy but I am new to reactive programming.


        public String getValue(Mono<String> monoString)
        // How to get actual String from param monoString 
        //and call print(String) method

        public void print(String str)

Getting a String from a Mono<String> without a blocking call isn't easy, it's impossible. By definition. If the String isn't available yet (which Mono<String> allows), you can't get it except by waiting until it comes in and that's exactly what blocking is.

Instead of "getting a String" you subscribe to the Mono and the Subscriber you pass will get the String when it becomes available (maybe immediately). E.g.

  value -> Console.out.println(value), 
  error -> error.printStackTrace(), 
  () -> Console.out.println("completed without a value")

will print the value or error produced by myMono (type of value is String, type of error is Throwable). At you can see other variants of subscribe too.

How to extract string from Mono<String> in reactor core, To answer the question directly in its simplest form - you use Mono.block() . But you almost certainly shouldn't, as this blocks the thread,  Mono<String> blockingHelloWorld() { return Mono.just("Hello world!"); } String result = blockingHelloWorld().block(); assertEquals("Hello world!", result); Here, we're blocking the execution as long as the publisher doesn't emit the value. However, it can take any amount of time to finish. To get more control, let's set an explicit duration: String result = blockingHelloWorld().block(Duration.of(1000, ChronoUnit.MILLIS)); assertEquals(expected, result);

According to the doc you can do:

String getValue(Mono<String> mono) {
    return mono.block();

be aware of the blocking call

How to Extract a Mono's Content in Java, Mono<String> blockingHelloWorld() {. return Mono.just( "Hello world!" ); } String result = blockingHelloWorld().block();. assertEquals( "Hello  One of the things which is wrong here is this like String id = (u -> u.get_id ()).toString ();. The toString will return you a String like "Mono@13254216541", because you are calling Mono.toString. One more thing, you shouldn't use the request's data in the body of your function, but in a map or flatMap function.

Finally what worked for me is calling flatMap method like below:

public void getValue(Mono<String> monoString)

Mono.Defer() vs Mono.create() vs Mono.just()?, instance. The evaluation of this expression is deferred until somebody subscribes. Do you have a work around with an example that I can get a String value from a Mono String? Or should I go back to SpringBoot 2.0.0? Copy link Quote reply

What worked for me was the following:


Project Reactor, is a stream of 0 to 1 element, whereas a Flux is a stream of 0 to N elements. 1 Answer1. If you are trying to return just a Mono object, you can use the flatMap method instead of map, so you can avoid something like Mono<Mono<X>> and get just Mono<X>. Transform the item emitted by this Mono by applying a synchronous function to it.


[Reactor Java #1] How to create Mono and Flux ?, Do you have a work around with an example that I can get a String value from a Mono String? Or should I go back to SpringBoot 2.0.0? Iterable<Mono<String>> monos = Flux<String> f = Flux.mergeSequential(monos); This kind of merge (sequential) will maintain the ordering inside given source iterable, and will also subscribe/request eagerly from all participating sources (so more parallelization expected while computing mono results). share.

Unwrap Mono String to a String value · Issue #351 · reactor/reactor , @PostMapping("/comments") public Mono<String> addComment(Mono<​Comment> newComment) { if (commentSink != null) { return newComment  Mongo Flux/Mono get object/s. Ask Question How to get an enum value from a string value in Java? 1145. Sort ArrayList of custom Objects by property. 760.

Java Code Examples reactor.core.publisher.Mono, I have a method which accepts Mono as a param. All I want is to get the actual String from it. Googled but didn't find answer except calling block() over Mono  Array Methods. Use the mono_array_new_* methods to create arrays of a given type.. For example, the following code creates an array with two elements of type System.Byte, and sets the values 0xca and 0xfe on it:

How to get String from Mono<String> in reactive java, @Nullable private String formatBody(@Nullable MediaType contentType, Mono<​byte[]> body) { return body .map(bytes -> { if (contentType == null) { return  Fanu showing how to make sure the sounds in your song will play at the desired level when the song is played in mono. CloudBounce is an online mastering service. Fanu is a mixing and mastering

  • could you explain to me what Mono is?
  • check ->
  • If you use the block(Duration timeout) with zero duration -…
  • That's what I am looking for-> how to subscribe it? May be via lambda?
  • Yes, that's one option. See the edit. But the point is that you can't write getValue method in your question.
  • Actually with string value I need to call another method... Let's say Mono<string> myMono has "abc" as actual string. With that String I need to call another method say print(String str) in that method. So how do I achieve it.. myMono.subscribe(value->print(value)) ?
  • Yes, just as I show with Console.out.println in the example.
  • I know usage of block but as it will make a blocking call I don't want to use it. Isn't there another way?
  • Just an FYI, instead of creating your own print(String str) method and doing this::print, you can simply do System.out::print
  • Why does this exact piece of code gives me an error: "Cannot infer type argument(s) for <R> flatMap(Function<? super T,? extends Mono<? extends R>>)" ??
  • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
  • What's User, monoUser and getId? None of them are in original question. So all you are trying is to solve an hypothetical query.