我正在 Spring/MongoDB 上构建反应式 RESTful 应用程序,我想在嵌套对象上构建反应式 CRUD 逻辑。
我有嵌套的Store对象:
@Document
public class Store {
@Id private String id;
private String url;
private List<Product> products;
//getters
//setters
}
@Document
public class Product {
private String name;
private int price;
//getters
//setters
}
服务类负责向客户端执行响应式请求:
@Service
public class Service {
private final WebClient webClient;
// initialize base url
public Service(WebClient.Builder webClientBuilder, String clientBaseUrl) {
this.webClient = webClientBuilder.baseUrl(clientBaseUrl).build();
}
// make GET request to get product collection
public Flux<Product> getProducts() {
return this.webClient.get()
.uri("/products")
.retrieve()
.bodyToFlux(Product.class);
}
}
Reactive StoreRepository存储库执行数据库 CRUD 逻辑:
@Repository
public interface StoreRepository extends ReactiveMongoRepository<Store, String> {
Mono<Store> findById(int storeId);
// perform GET request and save product collection in Store object
void saveProducts(int storeId,) {
Mono<Store> store = this.findById(storeId);
Service service = new Service(store.getUrl());
// handles GET request and returns product collection as Flux<Product>
Flux<Product> products = service.getProducts();
store.setProducts(products); //however, setProducts() accepts only List<Product>
this.save(store);
}
}
问题是如果该设置器仅接受格式数据,我该如何设置Flux<Product>
属性?store.setProducts()
List<Product>