簡體   English   中英

如何在項目反應器中組合來自兩個反應流的數據?

[英]How to combine data from two reactive streams in project reactor?

我最近開始使用 Project Reactor,但不知道如何使用嵌套流。 我想用內部單聲道的一些數據更新外部單聲道的數據。

 @GetMapping("/search")
    public Mono<Github> combineGithubData() {
        WebClient client = WebClient.create("https://api.github.com");
        Mono<Github> data = client.get().uri(URI.create("https://api.github.com/users/autocorrectoff")).retrieve().bodyToMono(Github.class);
        data = data.map(s -> {
            client.get().uri(URI.create("https://api.github.com/users/Kukilej")).retrieve().bodyToMono(Github.class).map(m -> {
                s.setXXX(m.getName());
                return m;
            });

            return s;
        });
        return data;
    }

字段 XXX 始終返回為 null,盡管我已將其設置為來自內部 Mono 的值。 我很確定這會在 RxJs 中起作用。 我如何使用 Project Reactor 進行這項工作?

編輯:Github類的代碼

import lombok.*;

@Getter @Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Github {
    private String login;
    private int id;
    private String node_id;
    private String avatar_url;
    private String gravatar_id;
    private String url;
    private String html_url;
    private String followers_url;
    private String following_url;
    private String gists_url;
    private String starred_url;
    private String subscriptions_url;
    private String organizations_url;
    private String repos_url;
    private String events_url;
    private String received_events_url;
    private String type;
    private boolean site_admin;
    private String name;
    private String company;
    private String blog;
    private String location;
    private String email;
    private String hireable;
    private String bio;
    private int public_repos;
    private int public_gists;
    private int followers;
    private int following;
    private String created_at;
    private String updated_at;
    private String XXX;

}

您的內部流沒有被訂閱。 要么我們flatMap ,要么更好,使用zip

data
    .zipWith(client.get().uri(...).retrieve().bodyToMono(Github.class))
    .map(tuple2 -> {
        //update tuple2.getT1() with m.getName() and return the updated tuple
        return tuple2.mapT1(tuple2.getT1().setXXX(tuple2.getT2().getName()));
    })
    .map(tuple2 -> tuple2.getT1() //updated s);

zipWith()訂閱內部流。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM