简体   繁体   中英

java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-kqueue-4

Im getting an error with the blocking operation in Spring Webflux. I retrieve a Mono of list of Address documents and im using this Mono list of address documents to form the street address(withStreet)as shown below:

Mono<List<Address>> docs = getAddress(id, name);

AddressResponse addrResponse = new AddressResponse.Builder().
                        withStreet(docs.map(doc -> doc.stream().
                                 map(StreetAddress::map).
                                collect(Collectors.toList())).block()).
                        build();

map method:

 public static StreetAddress map(Address addr) {
            return new Builder().
                withId(addr.getId()).
                withStreet(addr.getStreetAddress()).
                build();
        }

When i execute the above code, it throws a "block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-2". Could you please suggest how to fix. i want to retrieve AddressResponse without blocking it. This response will be further used in the code in Response Entity as shown below:

 return Mono.just(ResponseEntity
                .status(HttpStatus.OK)
                .body(addrResponse)));

The problem is you try to mix reactive and imperative code. Instead, just map it in the reactive pipeline:

Mono<AddressResponse> response = docs.map(addresses->{
  return new AddressResponse.Builder()
          .withStreet(addresses -> addresses.stream()
                                .map(StreetAddress::map)
                                .collect(Collectors.toList()))
           .build();
})

Then you can return it as is, or map it into a Mono> type, apply the same method then above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM