flatmap

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

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

  1. @Service
  2. ...
  3. public Flux<Boolean> checkUserHaveGroup(final List<String> friends) {
  4. MatchOperation match1 = Aggregation.match(Criteria.where("friends").in(friends).and("status").is("ACTIVE"));
  5. UnwindOperation unwind1 = Aggregation.unwind("friends");
  6. MatchOperation match2 = Aggregation.match(Criteria.where("friends").in(friends));
  7. GroupOperation group1 = Aggregation.group("friends");
  8. TypedAggregation<Group> a = Aggregation.newAggregation(
  9. Group.class,
  10. match1, unwind1, match2, group1);
  11. return this.reactiveMongoTemplate.aggregate(a, FriendInGroup.class)
  12. .map(friendInGroup -> friendInGroup.id)
  13. .collectList()
  14. .map(users -> haveNoGroupsList(users, friends))
  15. .flatMapMany(noGroupUsers -> {
  16. return Flux.fromIterable(noGroupUsers)
  17. .flatMap(pn -> crmService.deleteAttribute(pn, "joinedAGroup"));
  18. });
  19. }
  1. (this method get the user information)
  2. ...
  3. public Mono<UserInfo> userInfoById(final String userId) {
  4. return webClient.get()
  5. .uri(uriBuilder -> uriBuilder.path(constants.getByIdPath() + "/{id}")
  6. .build(userId))
  7. .header("auth", tokenService.token())
  8. .exchange()
  9. .flatMap(response -> {
  10. Mono<UserInfo> responseMono;
  11. if (response.statusCode().equals(HttpStatus.UNAUTHORIZED)) {
  12. responseMono = Mono.error(new UnauthorizedException());
  13. } else if (response.statusCode().equals(HttpStatus.OK)) {
  14. responseMono = response.bodyToMono(UserInfoResponse.class)
  15. .flatMap(uir -> Mono.just(uir.getData()));
  16. } else {
  17. responseMono = Mono.error(new UnhandledException());
  18. }
  19. return responseMono;
  20. });
  21. }
  1. ...
  2. private Mono<UserInfo> getUserInfo(String userId) {
  3. return userInfoAdapter.userInfoById(userId);
  4. }
  5. ...
  6. public Mono<Boolean> deleteAttribute(final String userId, final String attribute) {
  7. return getUserInfo(userId) <<<<< here we get the users info
  8. .flatMap(ui -> crmDeleteAttribute(ui, attribute)); <<<< this call is never done.
  9. }
  10. ...
  11. public Mono<Boolean> crmDeleteAttribute(final UserInfo user, final String attribute) {
  12. return webClient.delete()
  13. .uri(uriBuilder -> uriBuilder
  14. .path(contants.path())
  15. .build(user.getId(), attribute))
  16. .header("auth", tokenService.token())
  17. .exchange().flatMap(response -> {
  18. if (response.statusCode().equals(HttpStatus.OK)) {
  19. return Mono.just(Boolean.TRUE);
  20. }
  21. if (response.statusCode().equals(HttpStatus.BAD_REQUEST)) {
  22. return Mono.error(CrmServiceBadRequestException::new);
  23. }
  24. if (response.statusCode().equals(HttpStatus.UNAUTHORIZED)) {
  25. return Mono.error(CrmServiceUnauthorizedException::new);
  26. }
  27. return Mono.error(CrmServiceUnhandledException::new);
  28. });
  29. }

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

暂无答案!

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

相关问题