如何将 Mono<List> 对象转换为 java 中的简单 List 对象?

How to convert Mono<List> object to simple List object in java?

我正在尝试使用 JAVA web-flux 创建 POST API。在此 API 中,我想创建一个字符串列表并进行数据库调用以获取 Mono<List> 作为响应,但是当我尝试使用 [=16] 将 Mono<List> 转换为简单列表对象时=] 我收到以下回复:

{
    "code": 500,
    "message": "block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-8"
}

如何将 Mono 对象转换为简单的 List 对象?

我正在使用以下代码:

List<String> paymentInstrumentIdList = paymentInstrumentRequest.getPaymentInstruments().stream().map(PaymentInstrumentData::getPaymentInstrumentId).collect(Collectors.toList());
Mono<List<PaymentInstrument>> paymentInstrumentList = paymentInstrumentRepository.getByPartitionKey(partitionKey.toString(), DocType.PAYMENT_INSTRUMENT, paymentInstrumentIdList);
return ResponseEntity.ok(responseMapper.getResponseAsClass(graphQlQueryController.queryPaymentInstrumentsByPaymentInstrumentId(baseHeaders, personId, membershipId, paymentInstrumentList.block()), Wallet.class, "Wallet"));

webflux 应用程序的全部目的是您无法阻止

您要查找的是 flatMap 函数。

return paymentInstrumentRepository.getByPartitionKey(
    partitionKey.toString(), 
    DocType.PAYMENT_INSTRUMENT, paymentInstrumentIdList)
    .flatmap(list -> {
        return return ServerResponse.ok().body(responseMapper.getResponseAsClass(
    graphQlQueryController.queryPaymentInstrumentsByPaymentInstrumentId(
        baseHeaders, 
        personId, 
        membershipId, 
        list);
    };

这是 webflux 的基础知识,我建议您阅读 official reactor documentation getting started 以便了解基础知识和目的。

答案是在手机上写的,未经测试。

这里还有一个getting started tutorial