WebFlux - 如何在服务方法中使用Flux<GroupTreeItem> 作为List<GroupTreeItem> 的参数。

问题描述 投票:0回答:1

我使用Spring Boot和Webflux。我想知道如何使用我的allUserGroups.NET Framework 2.0。

             public Mono<ResponseEntity<Flux<GroupTreeItem>>> userAllTreeGroups(
 //1
                 Flux<UUID> groupIds = userGroupService
    .findUserGroupByEmail(email)
    .flatMap(groupById -> userGroupService.findUserTreeGroup(groupById.getId(), email))
    .map(GroupTreeItem::getId);

    //2
             rolePrivilegesService
                    .filterGroupIdsForUserPrivilege(Arrays.asList(allUserGroups?????????), "group.permission.all")
                    .flatMap(filteredGroupId -> userGroupService.findUserTreeGroup(filteredGroupId, email))
                    .map(ResponseEntity::ok)
                    .defaultIfEmpty(ResponseEntity.notFound().build());
        }

where:

 public Mono<List<UUID>> filterGroupIdsForUserPrivilege(List<UUID> groupIds, String privilege) 

public class GroupTreeItem   {
  @JsonProperty("id")
  private UUID id;
...
...}

RECAP:我的问题是如何通过

Flux<UUID> groupIds

到。

rolePrivilegesService
                    .filterGroupIdsForUserPrivilege(myListOfUUID, "group.permission.all")

而这里的身体

userGroupService.findUserTreeGroup。

 public Mono<GroupTreeItem> findUserTreeGroup(UUID groupId, String email) {
    return groupByIdRepo.findById(groupId).flatMap(group -> findAndPopulateChildData(email, group));
      }

如何在Webflux中实现不断链的非阻塞操作?

stream spring-webflux
1个回答
2
投票

如前所述 K.Nicholas 在评论中,你需要将Flux转化为Mono>由。Flux::collectToList 操作,并像这样在链中传播你的列表。

userGroupService
    .findUserGroupByEmail(email)
    .flatMap(groupById -> userGroupService.findUserTreeGroup(groupById.getId(), email))
    .map(GroupTreeItem::getId)
    .collectToList()
    .flatMap(myListOfUUID -> rolePrivilegesService.filterGroupIdsForUserPrivilege(myListOfUUID, "group.permission.all"))
    .flatMapMany(Flux::fromIterable)  
    .flatMap(filteredGroupId -> userGroupService.findUserTreeGroup(filteredGroupId, email))
    .map(ResponseEntity::ok)
    .defaultIfEmpty(ResponseEntity.notFound().build());

如果你需要将Mono> 转化为Flux,就使用 .flatMapMany(Flux::fromIterable) 不妨换个方法 rolePrivilegesService.filterGroupIdsForUserPrivilege 返回 Flux

© www.soinside.com 2019 - 2024. All rights reserved.