1

I am trying to create a POST API using JAVA web-flux. In this API I want to create a list of string and make a database call to get Mono<List> as a response but when I am trying to convert Mono<List> to simple list object using block() I'm getting below response:

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

How can I convert Mono object to simple List object?

I'm using below code:

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"));
1

1 Answer 1

4

The entire purpose with webflux applications is that you cannot block

what you are looking for is the flatMap function.

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

This is basic knowledge in webflux i suggest you read the official reactor documentation getting started so you understand the basics and the purpose.

The answer is written on mobile and not tested.

Here is also a getting started tutorial

Sign up to request clarification or add additional context in comments.

2 Comments

Getting error on build() method - java: cannot find symbol symbol: method build() location: class java.lang.object
it was written on mobile and not tested so i removed the code, and not to be used to just copy and paste, you get the gist and can look up into the documentation how to return a correct server response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.