[英]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.