简体   繁体   English

如何退回 Mono <list<object> &gt; 变成 HashMap? </list<object>

[英]How do I return a Mono<List<Object>> into a HashMap?

I have a hashmap of <Integer, QueryObj> that I need to iterate over and call an external service with.我有一个 <Integer, QueryObj> 的 hashmap,我需要对其进行迭代并调用外部服务。 The method signature of my external service is like:我的外部服务的方法签名如下:

private Mono<List<ReturnedObj>> fetchList(QueryObj query)

and I've been able to verify that it's working and returns a list of what I need.我已经能够验证它是否正常工作并返回我需要的列表。 However, I'm unsure of what my next steps should be and what my response type in the parent method should be in order to maintain reactive practices.但是,我不确定我的下一步应该是什么,以及我在父方法中的响应类型应该是什么,以保持反应性实践。 Basically, I want to transform the Map<Integer, Query> into Map<Integer, Mono<List<ReturnedObj>> .基本上,我想将Map<Integer, Query>转换为Map<Integer, Mono<List<ReturnedObj>> I am wondering if Map<Integer, Mono<List<ReturnedObj>> is even possible?我想知道Map<Integer, Mono<List<ReturnedObj>>是否可能? Does it need to be Mono<Map<K<V>> ?它需要是Mono<Map<K<V>>吗?

Here is the current code snippet - it doesn't throw an error, but rather returns an empty result.这是当前的代码片段——它不会抛出错误,而是返回一个空结果。 I'm thinking the mix of imperative and reactive programming doesn't wait for the results of fetchList() to populate the response.我认为命令式和反应式编程的混合不会等待fetchList()的结果来填充响应。

Map<Integer, QueryObj> queryMap = getQueries(); // setup
return queryMap.entrySet()
                    .stream()
                    .collect(Collectors.toMap(
                            e -> e.getKey(), e -> {
                                try {
                                     return fetchList(e.getValue());
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                }
                                return null;
                            }));
        }

Would greatly appreciate any help.非常感谢任何帮助。 I am fairly new to this.我对此很陌生。

Thanks all for the suggestions - I ended up finding a solution that works for me.感谢所有人的建议 - 我最终找到了适合我的解决方案。 I ended up making my whole flow reactive (as I should have done from the start) and changed the return type of my method to be Mono<Map<Integer, List>.我最终使我的整个流程具有反应性(就像我从一开始就应该做的那样)并将我的方法的返回类型更改为 Mono<Map<Integer, List>。 I used a Flux.fromIterable() with flatMap() on the entrySet and collectMap to get the results together into a map.我在 entrySet 和 collectMap 上使用了Flux.fromIterable()flatMap()来将结果汇总到 map 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM