繁体   English   中英

如何使用弹簧通量单声道过滤两次

[英]How to filter twice using spring flux mono

我想过滤一次以确保 object.getApiKey() 等于我的类的私有 apiKey 变量,然后如果数据库表行中不存在 object.getEmail() 也进行过滤。

我尝试了几种过滤方法,但到目前为止唯一正在编译的是以下代码:

public Mono<String> signupUser(Mono<UserTransfer> userMono) {
        LOG.info("signup user");

        return userMono
                .filter(userTransfer -> {
                    // first filter on apiKey
                    LOG.info(" userTransfer.apiKey: {}, apiKey: {}, match: {}", userTransfer.getApiKey(), apiKey, userTransfer.getApiKey().equals(apiKey));

                    return userTransfer.getApiKey().equals(apiKey);
                })
                .flatMap(userTransfer -> {

                    //I zip the mono found in the repository with the userTransfer so I can reuse it later in the next steps
                    Mono<Tuple2<User, UserTransfer>> tupleMono = userRepository.findByEmail(userTransfer.getEmail()).zipWith(Mono.just(userTransfer));
                    
                    LOG.info("do findByEmail for email: {}", userTransfer.getEmail());

                    return tupleMono;
                })              
                .filter(objects -> {
                    //2nd filter  
                    //however this line is never executed when I run my integ test. Any clue?
                    LOG.info("user findByEmail is {}, should be null since we don't allow user if email is already used.", objects.getT1());
                    
                return objects.getT1() == null;
                } )
                .flatMap(objects -> {
                    User user = new User(objects.getT2().getFirstName(), objects.getT2().getLastName(), objects.getT2().getEmail());
                    return userRepository.save(user).zipWith(Mono.just(objects.getT2()));
                    })


                .flatMap(objects -> {
                    User user = objects.getT1();
                    UserTransfer userTransfer = objects.getT2();
            LOG.info("create authentication with rest call to endpoint {}", authenticationEp);

            WebClient.ResponseSpec responseSpec = webClient.post().uri(authenticationEp).bodyValue(userTransfer).retrieve();
            return responseSpec.bodyToMono(String.class).map(authenticationId -> {
                LOG.info("got back authenticationId from service call: {}", authenticationId);
                return "user created with id: " + user.getEmail() + " with authId: " + authenticationId;
            });
        });
    }
}

似乎您需要检查元组中用户对象的“空性”,因为项目反应器的设计对象不能为空。

尝试检查从存储库返回的对象的内容。 祝你好运

在此处输入图像描述

暂无
暂无

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

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