有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring Boot WebFlux在运行i parallel时使用id保留结果的上下文

使用Spring boot Web client从REST API请求JSON,服务器的响应体存储在一个简单的Map中。对服务器的请求包含一个ID,但该ID不包括在响应中,而是解释响应所必需的

我希望在并行调用的结果中包含id,因此它应该是Map<UUID,Map> data或类似的,而不是List<Map> data。因此,来自服务器的响应与用于获取响应的id配对

        List<Map> data = Flux.fromIterable(ids)
                .parallel()
                .runOn(Schedulers.elastic())
                .flatMap(this::callAPI).sequential().collectList().block();
    private Mono<Map> callAPI(UUID id) {
        return client.get().uri(uriBuilder -> uriBuilder
                .path("/{id}/")
                .build(id))
                .retrieve().bodyToMono(Map.class);
    }

共 (1) 个答案

  1. # 1 楼答案

    来自@ThomasAndolf comment

    By not returning a Mono<Map> from your callApi function. Return a Map and the Id in wrapper object that you can later use to remap to your wanted structure .bodyToMono(Map.class).flatMap(map -> { return Mono.just(new Response(id, map)); }); – Thomas Andolf 22 hours ago