Spring Webflux Mono:无法将结果收集为列表

问题描述 投票:0回答:1

我正在使用spring webflux来消耗彼此的服务,并尝试将结果汇总到不同输入组合的列表中。但是遇到了我无法订阅并将结果转换为List的问题[]

  1. 此方法将获取一个Map并遍历该Map并通过不同的输入组合调用另一个服务。但是无法在getAPrice()中完成我想要的事情:(

    输入:

     public Mono<List<APrice>> getAPrice() {
            return
                    Mono.create(monoSink ->{
                         getActiveMonths()
                                .entrySet().stream()
                                .flatMap(e->getContractPrice(e.getKey(),e.getValue())
                                /* stuck here, don't have any idea how to subccribe and get the AP Object and collect it as a list */
                    });
        } 
    

    输出:

    expected : List[APrice]    
    
  2. 上面的getAPrice()调用下面的方法,该方法可以正常工作并返回有效输入组合的数据

  3. 输入:

     /*Method is Working fine and returns a Mono<APrice> for the right input combination */
    public Mono<APrice> getContractPrice(String key, String value){
        return Mono.create(monoSink ->{
            acServiceWebClient.getPrice(key,value) 
                    .subscribe(aPrice ->  monoSink.success(aPrice),
                            error->{
                                monoSink.error(error);
                            });
        });
    }
    

    输出:

    {
    "id": 11,
    "curveName": "NT",
    "Month": "Mar 2010",
    "price": 160.17,
    "status": "ACTIVE"
    }
    
        private Map<String,String> getActiveMonths()
        {
            Map hm = new HashMap();
            hm.put("key1","value1")
            hm.put("key2","value2")
            return hm;
        }
    

期待获得一些更好的方法来完成getAPrice()的建议,以防我采用错误的方法纠正我。谢谢

我正在使用spring webflux来消耗彼此的服务,并尝试将结果汇总到不同输入组合的列表中。但是卡在了我无法订阅和转换的地方……

java stream spring-webflux
1个回答
0
投票

您实际上不需要工作MonoSink。您可以从Flux getActiveMonths创建一个entrySet,然后使用该getActivePriceentrySetkey调用方法value。因此,您的getAPrice实际上应该看起来像

© www.soinside.com 2019 - 2024. All rights reserved.