Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebFlux Chaining from Method that Returns Mono<Void>

Here is my controller for deleting an item:

 public Mono<ResponseEntity> delete(
        @PathVariable(value = "id") String id) {
    return itemService.delete(id)
            .map(aVoid -> ResponseEntity.ok());
}

itemService.delete(id) returns Mono<Void>

But when i succesfully deleted an item, it is not giving me the response entity object. It only returns blank json.

I seems that map is not executed because delete method returns Mono<Void>

How to do this correctly?

like image 780
user1955934 Avatar asked Sep 03 '19 05:09

user1955934


People also ask

Is Mono void same as mono empty?

How do these stand different in their usage ? Mono<Void> is a type. Mono. empty() is a method invocation that returns a Mono that that completes without emitting any item.

What does Mono empty do?

Mono. empty() is a method invocation that returns a Mono that completes emitting no item. It represents an empty publisher that only calls onSubscribe and onComplete . This Publisher is effectively stateless, and only a single instance exists.


1 Answers

A reactive streams publisher can send 3 types of signals: values, complete, error. A Mono<Void> publisher is way to signal when an operation is completed - you're not interested in any value being published, you just want to know when the work is done. Indeed, you can't emit a value of a Void type, it doesn't exist. The map operator you're using transforms emitted values into something else.

So in this case, the map operator is never called since no value is emitted. You can change your code snippet with something like:

public Mono<ResponseEntity> delete(
        @PathVariable(value = "id") String id) {
    return itemService.delete(id)
            .then(Mono.just(ResponseEntity.ok()));
}
like image 185
Brian Clozel Avatar answered Nov 15 '22 23:11

Brian Clozel