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?
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.
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.
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()));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With