我使用springboot + mongodb,我是webflux的初学者。我为CRUD编写代码。当我访问控制器中的删除id时,代码不工作,因为计数总是返回0。有人帮我吗?
@ApiOperation(value = "Delete multi cities")
@DeleteMapping
public Mono<ResponseEntity<AtomicInteger>> deleteByIds(@RequestBody @NotNull Set<String> ids) {
AtomicInteger count = new AtomicInteger(0);
Flux.fromIterable(ids)
.flatMap((id) -> {
return cityService.findById(id)
.flatMap((c) -> {
count.getAndAdd(1);
return cityService.deleteById(c.getId());
});
});
log.debug("count = {}", count);
return Mono.just(ResponseEntity.ok(count));
}
未订阅Flux你应该像这样试试
return Flux.fromIterable(ids)
.flatMap((id) -> {
return cityService.findById(id)
.flatMap((c) -> {
count.getAndAdd(1);
return cityService.deleteById(c.getId());
});
})
.then(Mono.defer(() -> {
log.debug("count = {}", count);
return Mono.just(ResponseEntity.ok(count));
}));