flatmap

zaq34kh6  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(318)

我在SpringWebFlux的@service中有这个函数,它通过朋友列表调用,以了解每个人是否都加入了一个组。如果一些朋友没有群组,这个方法调用一个api来获取其用户信息,然后调用另一个api来用joined tag false标记这些人。

@Service
...
  public Flux<Boolean> checkUserHaveGroup(final List<String> friends) {
    MatchOperation match1 = Aggregation.match(Criteria.where("friends").in(friends).and("status").is("ACTIVE"));
    UnwindOperation unwind1 = Aggregation.unwind("friends");
    MatchOperation match2 = Aggregation.match(Criteria.where("friends").in(friends));
    GroupOperation group1 = Aggregation.group("friends");

    TypedAggregation<Group> a = Aggregation.newAggregation(
        Group.class,
        match1, unwind1, match2, group1);

    return this.reactiveMongoTemplate.aggregate(a, FriendInGroup.class)
        .map(friendInGroup -> friendInGroup.id)
        .collectList()
        .map(users -> haveNoGroupsList(users, friends))
        .flatMapMany(noGroupUsers -> {
          return Flux.fromIterable(noGroupUsers)
              .flatMap(pn -> crmService.deleteAttribute(pn, "joinedAGroup"));
        });
  }
(this method get the user information)
...
public Mono<UserInfo> userInfoById(final String userId) {

    return webClient.get()
        .uri(uriBuilder -> uriBuilder.path(constants.getByIdPath() + "/{id}")
            .build(userId))
        .header("auth", tokenService.token())
        .exchange()
        .flatMap(response -> {
          Mono<UserInfo> responseMono;

          if (response.statusCode().equals(HttpStatus.UNAUTHORIZED)) {
            responseMono = Mono.error(new UnauthorizedException());
          } else if (response.statusCode().equals(HttpStatus.OK)) {
            responseMono = response.bodyToMono(UserInfoResponse.class)
                .flatMap(uir -> Mono.just(uir.getData()));
          } else {
            responseMono = Mono.error(new UnhandledException());
          }

          return responseMono;
        });

  }
...
  private Mono<UserInfo> getUserInfo(String userId) {
    return userInfoAdapter.userInfoById(userId);
  }

...

  public Mono<Boolean> deleteAttribute(final String userId, final String attribute) {
    return getUserInfo(userId) <<<<< here we get the users info
        .flatMap(ui -> crmDeleteAttribute(ui, attribute)); <<<< this call is never done.
  }

...

 public Mono<Boolean> crmDeleteAttribute(final UserInfo user, final String attribute) {

    return webClient.delete()
        .uri(uriBuilder -> uriBuilder
            .path(contants.path())
            .build(user.getId(), attribute))
        .header("auth", tokenService.token())
        .exchange().flatMap(response -> {
          if (response.statusCode().equals(HttpStatus.OK)) {
            return Mono.just(Boolean.TRUE);
          }
          if (response.statusCode().equals(HttpStatus.BAD_REQUEST)) {
            return Mono.error(CrmServiceBadRequestException::new);
          }
          if (response.statusCode().equals(HttpStatus.UNAUTHORIZED)) {
            return Mono.error(CrmServiceUnauthorizedException::new);
          }
          return Mono.error(CrmServiceUnhandledException::new);
        });
  }

在api上获取用户信息后,无论我做什么,都不会调用用于分配标记的api。我可以在调试器终端中看到对userinfo api的调用已经完成,但在那之后,应用程序返回到控制器。有人能指出我做错了什么?
欢迎任何帮助。
谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题