从返回Mono <Void>的方法进行WebFlux链接

use*_*934 1 java-8 project-reactor spring-webflux

这是我删除项目的控制器:

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

itemService.delete(id) 退货 Mono<Void>

但是,当我成功删除一个项目时,它没有给我响应实体对象。它仅返回空白json。

我似乎未执行地图,因为delete方法返回 Mono<Void>

如何正确做到这一点?

Bri*_*zel 5

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()));
}
Run Code Online (Sandbox Code Playgroud)