java - 如何在项目 react 器中组合来自两个 react 流的数据?

标签 java spring-boot reactive-programming 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() 订阅内部流。

关于java - 如何在项目 react 器中组合来自两个 react 流的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59115909/

相关文章:

java - Spring Boot :org. springframework.beans.factory.UnsatisfiedDependencyException

java - 如何从 BehaviorSubject 获取最新值?

javascript - 在 React 中创建更多元素

java - JMenu子菜单鼠标监听没有监听; Action 监听器做了——为什么?

java - Android 上的简单 XML - 类 MyArrayList<E> 扩展了 ArrayList<E>?

java - 如何从内部类继承?

mysql - Json响应与数据库数据不匹配

java - 通过 JPA 将记录插入数据库

java - 如何让 FlyWay 运行我的迁移? "Schema is up to date. No migration necessary."

ios - react native中SectionList的访问与理解