user1955934
user1955934

Reputation: 3495

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?

Upvotes: 5

Views: 8414

Answers (1)

Brian Clozel
Brian Clozel

Reputation: 59086

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()));
}

Upvotes: 21

Related Questions