我希望在某个人最喜欢的商店找到所有产品,并发现在reactive方式编码时有许多嵌套代码。这是正确的方法吗?或者,如果有更好的方法?
@Component
public class ProductController {
@Autowired
ShopRepo shopRepo;
@Autowired
ProductRepo productRepo;
public Mono<ServerResponse> products(ServerRequest req) {
String userid = req.queryParam("userid");
Flux<Shop> shops = shopRepo.favorite(uid);
return shops
.flapMap(s -> Mono.just(s.getId()))
.collectList()
.flapMap(list -> {
String[] shopIds = list.toArray(new String[list.size()]);
return ServerResponse.ok().body(productRepo.findByShops(shopIds), Product.class);
});
}
}