spring webflux只能处理一个反应类型,不能处理嵌套的反应类型(比如
Mono<Flux<Integer>>
)中。你的控制器方法可以返回
Mono<Something>
,一个
Flux<Something>
,一个
ResponseEntity<Mono<Something>>
,一个
Mono<ResponseEntity<Something>>
,等等-但从不嵌套反应类型。
您在响应中看到的奇怪数据实际上是jackson试图序列化一个反应类型(所以您看到的是数据的承诺,而不是数据本身)。
在这种情况下,可以这样重写方法:
@GetMapping("/integers")
@ResponseStatus(code = HttpStatus.OK)
public Mono<Map<String, Flux<Integer>>> getIntegers() {
Flux<Integer> integers = integerService.getIntegers();
Mono<Map<String, List<Integer>>> result = integers
// this will buffer and collect all integers in a Mono<List<Integer>>
.collectList()
// we can then map that and wrap it into a Map
.map(list -> Collections.singletonMap("Integers", list));
return result;
}
您可以在中阅读有关支持的返回值的更多信息
the Spring WebFlux reference documentation
是的。