[英]How do I return a Mono after Flux's onComplete is executed?
I am trying to save a set of categories.我正在尝试保存一组类别。 When all categories are saved, set the categories of product to it.保存所有类别后,将产品类别设置为它。 Then return Product.然后返回产品。 So far I've managed to do this.到目前为止,我已经成功地做到了这一点。
public Mono<Product> save(Product product) {
final Set<Category> categories = product.getCategories();
Set<Category> _categorySet = new HashSet<>();
Mono<Product> _product;
for (Category category : categories) {
final Mono<Category> save = categoryService.save(category);
save.subscribe(_categorySet::add,null,()->{
product.setCategories(_categorySet);
repository.save(product);
});
}
}
How do I return product after it has been saved without relying on block()
?保存后如何在不依赖block()
的情况下退回产品? I cannot seem to find a source to learn these stuffs.我似乎找不到学习这些东西的来源。 Can someone point me to good materials.谁能指点我的好材料。
don't block webflux chain manually.不要手动阻止 webflux 链。 try to use this code.尝试使用此代码。
public Mono<Product> save(Product product) {
final Set<Category> categories = product.getCategories();
return Flux.fromIterable(categories)
.flatMap(categoryService::save)
.collect(Collectors.toSet())
.flatMap(categorySet -> { // use `flatMap` if repository.save(product); returns `Mono<Product>`. or else use `map` if repository.save(product); returns `Product`
product.setCategories(categorySet);
return repository.save(product);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.