Spring boot 转换通量<;列表<;T>&燃气轮机;列出<;T>;

Spring boot 转换通量<;列表<;T>&燃气轮机;列出<;T>;,spring-boot,spring-webflux,project-reactor,reactive,Spring Boot,Spring Webflux,Project Reactor,Reactive,我正在调用对DB的异步调用,以获取与用户相关的数据,并将其收集到如下列表中 Mono<AppUser> user = email.flatMap(s -> appUserRepository.findByEmailLikeIgnoreCase(Mono.just(s))); List<String> userAppRoles = new ArrayList<>(); user .map(AppUser::getApps) .subscribe

我正在调用对DB的异步调用,以获取与用户相关的数据,并将其收集到如下列表中

Mono<AppUser> user = email.flatMap(s -> appUserRepository.findByEmailLikeIgnoreCase(Mono.just(s)));
List<String> userAppRoles = new ArrayList<>();
user
   .map(AppUser::getApps)
   .subscribe(app -> app.forEach(x->userAppRoles.add(x.getAppRole())));
Mono user=email.flatMap->appUserRepository.findByEmailLikeIgnoreCase(Mono.just(s));
List userAppRoles=new ArrayList();
用户
.map(AppUser::getApps)
.subscribe(app->app.forEach(x->userAppRoles.add(x.getAppRole()));
但是,“UserApprovles”列表没有填充。 我必须以非阻塞的方式进行操作,因此不能使用block()或get()方法,这最终会导致错误,因为netty抱怨任何阻塞操作

    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {


        String path = exchange.getRequest().getURI().getPath();
        String method = exchange.getRequest().getMethodValue();
        List<ApplicationData> appData = new ArrayList<>();
        Mono<String> email = oidcuserService.getUsername();

        Flux<Router> routes = routerRepository.findAll();
        Mono<AppUser> user = email.flatMap(s -> appUserRepository.findByEmailLikeIgnoreCase(Mono.just(s)));
        routes.filter(router -> router.getPolicies().containsKey(method))
                .map(Router::getPolicies)
                .map(p -> p.get(method))
                .subscribe(appData::addAll);

        List<String> routerAppNames = appData.stream().map(ads -> ads.getAppName()).collect(Collectors.toList());
        List<String> routerAppRoles = appData.stream().map(ApplicationData::getAppRole).collect(Collectors.toList());
        List<String> routerModuleRoles = appData.stream().map(ApplicationData::getModuleRole).collect(Collectors.toList());
        List<String> routerPathcompRoles = appData.stream().map(ApplicationData::getPathCompRole).collect(Collectors.toList());
        List<String> userAppRoles = new ArrayList<>();
        List<String> userModuleRoles = new ArrayList<>();
        List<String> userPathcompRoles = new ArrayList<>();

        user.map(AppUser::getAppsList).subscribe(applicationData -> applicationData.forEach(x->userAppRoles.add(x.getAppRole())));
        user.map(AppUser::getAppsList).subscribe(applicationData -> applicationData.forEach(x->userModuleRoles.add(x.getModuleRole())));
        user.map(AppUser::getAppsList).subscribe(applicationData -> applicationData.forEach(x->userPathcompRoles.add(x.getPathCompRole())));

    boolean appRoleFlag = userAppRoles.stream().filter(routerAppRoles::contains).collect(Collectors.toList()).isEmpty();
    boolean moduleRoleFlag = userModuleRoles.stream().filter(routerModuleRoles::contains).collect(Collectors.toList()).isEmpty();
    boolean pathCompRoleFlag = userPathcompRoles.stream().filter(routerPathcompRoles::contains).collect(Collectors.toList()).isEmpty();

    boolean result = appRoleFlag & moduleRoleFlag & pathCompRoleFlag;
        if(!result) {
            ServerHttpResponse response = exchange.getResponse();
            response.setStatusCode(HttpStatus.FORBIDDEN);
            return response.setComplete();
        }
        return chain.filter(exchange);
    }
公共Mono筛选器(服务器WebExchange exchange、WebFilterChain链){
字符串路径=exchange.getRequest().getURI().getPath();
字符串方法=exchange.getRequest().getMethodValue();
List appData=new ArrayList();
Mono email=oidcuserService.getUsername();
Flux routes=routerRepository.findAll();
Mono user=email.flatMap->appUserRepository.findByEmailLikeIgnoreCase(Mono.just(s));
routes.filter(路由器->路由器.GetPolicys().containsKey(方法))
.map(路由器::GetPolicys)
.map(p->p.get(方法))
.subscribe(appData::addAll);
列出routerAppNames=appData.stream().map(ads->ads.getAppName()).collect(Collectors.toList());
List routerAppRoles=appData.stream().map(ApplicationData::getAppRole).collect(Collectors.toList());
列出routerModuleRoles=appData.stream().map(ApplicationData::getModuleRole).collect(Collectors.toList());
列出RouterPathCompoles=appData.stream().map(ApplicationData::GetPathCompole.collect(Collectors.toList());
List userAppRoles=new ArrayList();
List userModuleRoles=new ArrayList();
List userpathcompoles=newarraylist();
订阅(applicationData->applicationData.forEach(x->userAppRoles.add(x.getAppRole()));
订阅(applicationData->applicationData.forEach(x->userModuleRoles.add(x.getModuleRole()));
订阅(applicationData->applicationData.forEach(x->userpathcmproles.add(x.getpathcmprole()));
布尔appRoleFlag=userAppRoles.stream().filter(routerAppRoles::contains).collect(Collectors.toList()).isEmpty();
布尔moduleRoleFlag=userModuleRoles.stream().filter(routerModuleRoles::contains).collect(Collectors.toList()).isEmpty();
布尔pathCompRoleFlag=userPathcompRoles.stream().filter(routerPathcompRoles::contains).collect(Collectors.toList()).isEmpty();
布尔结果=appRoleFlag&moduleRoleFlag&pathCompRoleFlag;
如果(!结果){
ServerHttpResponse response=exchange.getResponse();
response.setStatusCode(HttpStatus.FORBIDDEN);
返回response.setComplete();
}
返回链。过滤器(交换);
}

考虑到Ricard Kollcaku在下面的评论,即整个代码需要以函数的方式编写,如何计算多个热流并返回一个布尔值而不是某个流的输出。

您要寻找的始终是阻塞。因为您正试图阻止流以获取一些数据。如果你想使用被动语态,你应该在你的情况下做每一部分的被动语态,以返回一个通量,如果不是的话,你应该使用命令语态one@RicardKollcaku对编辑后的问题有任何建议都很好。首先,不要使用任何订阅。立即返回流。尝试在可以使用的流中工作。collectList(),然后在flatmap中工作。您可以在flatmap中过滤3个流,并使用Flux.zip收集它们。OneEmpty您可以在flatmap返回响应中返回。setComplete();。作为回应,您可以返回链。过滤器(交换)您可以将其拆分为小函数,以便更易于理解。非常感谢。是的,我在一个方法中添加了太多的代码,将对其进行折射。您要查找的始终是阻塞。因为您正试图阻止流以获取一些数据。如果你想使用被动语态,你应该在你的情况下做每一部分的被动语态,以返回一个通量,如果不是的话,你应该使用命令语态one@RicardKollcaku对编辑后的问题有任何建议都很好。首先,不要使用任何订阅。立即返回流。尝试在可以使用的流中工作。collectList(),然后在flatmap中工作。您可以在flatmap中过滤3个流,并使用Flux.zip收集它们。OneEmpty您可以在flatmap返回响应中返回。setComplete();。作为回应,您可以返回链。过滤器(交换)您可以将其拆分为小函数,以便更易于理解。非常感谢。是的,我在一个方法中添加了太多的代码,将对其进行折射。