【问题标题】:Is it possible to insert content from one flux to another flux based upon some filter是否可以根据某些过滤器将内容从一种助焊剂插入到另一种助焊剂
【发布时间】:2018-10-10 12:30:58
【问题描述】:

我想根据某些条件将一个通量的内容填充到另一个通量中。这是我的两个助焊剂:

Flux<templateVersionBo> templateVerion = templateVersionRepo.findAll();
Flux<templateBo> template = templateRepo.findAll();

templateVersion 包含模板的 id。我想将模板版本填充到模板变量中,例如模板“publishedVersion”并返回 templateBo 通量,如下所示:

    if (templateVersion.getTemplateId().equals(template.getId()) {
            templateBo.setTemplatePublishedVersion(version);
    }
return templateBo;

我是响应式编程的新手。因此,即使不使用阻止/订阅,也不确定是否可能。

【问题讨论】:

    标签: java reactive-programming spring-webflux


    【解决方案1】:

    如果您的idLong 并且TemplateBo 的版本为“wither”,您可以像这样映射您的模板:

        // async collect id -> version map
        Mono<Map<Long, TemplateVersionBo>> templateVersionsMapMono =
                templateVerion.collectMap(TemplateVersionBo::getTemplateId);
    
        // get version from map by template id
        Flux<TemplateBo> templateBoFlux = templateVersionsMapMono
                .flatMapMany(templateVersionsMap -> template
                        .map(templateBo -> templateBo.withTemplatePublishedVersion(
                                templateVersionsMap.get(templateBo.getId()).getVersion())));
    

    【讨论】:

      【解决方案2】:

      最后这种方法对我有用:

      Mono<TemplateBo> templateBoMono = templateVersion
                  .collectMap(version -> version.getTemplateId())
                  .flatMap(publishedMap ->
                      template.map(templateBo -> {
                          templateBo.setPublished(publishedMap.get(templateId));
                          return templateBo;
                      }));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-29
        • 2016-02-05
        • 2015-03-22
        • 1970-01-01
        • 1970-01-01
        • 2016-03-12
        相关资源
        最近更新 更多