我有两个要求:
pip install --upgrade pip
对于这些,我想执行以下操作:
Flux<ProductID> getProductIds() {
return this.webClient.get()
.uri(PRODUCT_ID_URI)
.accept(MediaType.APPLICATION_STREAM_JSON)
.retrieve()
.bodyToFlux(ProductID.class);
}
Mono<Product> getProduct(String id) {
return this.itemServiceWebClient.get()
.uri(uriBuilder -> uriBuilder.path(PRODUCT_URI + "/{id}")
.build(id))
.accept(MediaType.APPLICATION_STREAM_JSON)
.exchange()
.flatMap(clientResponse -> clientResponse.bodyToMono(Product.class));
}
当我打电话给我时,得到以下输出:
Flux<Product> getProducts() {
return Flux.create(sink -> this.gateway.getProductIds()
.doOnComplete(() -> {
log.info("PRODUCTS COMPLETE");
sink.complete();
})
.flatMap(productId -> this.getProduct(productId.getID()))
.subscribe(product -> {
log.info("NEW PRODUCT: " + product);
sink.next(product);
}));
}
当然,由于异步单声道映射,流在结果实际到达之前就关闭了。我该如何保持这种非阻塞状态,同时又确保结果在调用on complete之前到达?
答案 0 :(得分:1)
假设getProducts
是一种控制器方法,并且您想在模型中添加要在视图模板中呈现的产品,则可以解决以下问题:
@GetMapping("/products")
public String getProducts(Model model) {
Flux<Product> products = this.gateway.getProductIds()
.flatMap(productId -> this.getProduct(productId.getID()));
model.put("products", products);
// return the view name
return "showProducts";
}