执行 Flux 的 onComplete 后如何 return 一个 Mono?
How do I return a Mono after Flux's onComplete is executed?
我正在尝试保存一组类别。当所有类别都保存好后,将产品的类别设置为它。然后 return 产品。到目前为止,我已经设法做到了。
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);
});
}
}
如何在不依赖block()
的情况下return保存产品?我似乎找不到学习这些东西的来源。谁能给我指点好的资料。
不要手动阻止 webflux 链。尝试使用此代码。
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);
});
}
我正在尝试保存一组类别。当所有类别都保存好后,将产品的类别设置为它。然后 return 产品。到目前为止,我已经设法做到了。
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);
});
}
}
如何在不依赖block()
的情况下return保存产品?我似乎找不到学习这些东西的来源。谁能给我指点好的资料。
不要手动阻止 webflux 链。尝试使用此代码。
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);
});
}