简体   繁体   中英

Inappropriate blocking method call (using reactor.core.publisher.Mono<T>)

can you help me understand why you give me this warning?

Essentially this POST call gives me results (userPayload), that I reuse in a further POST call and save the data to db.

What's wrong?

public Mono<ResponseEntity> createUser(UserRequest requestPayload) {

   return webClientBuilder
            .build()
            .post()
            .uri(settings.getUrl())
            .accept(MediaType.APPLICATION_JSON)
            .contentType(MediaType.APPLICATION_JSON)
            .header("Authorization", settings.getApiToken())
            .body(BodyInserters.fromValue(requestPayload))
            .exchange()
            .flatMap(clientResponse -> {
                if (clientResponse.statusCode().isError()) {
                    return clientResponse.bodyToMono(Error.class)
                            .flatMap(error -> Mono.error(new CustomException(clientResponse.statusCode(), error)));
                } else {
                    return clientResponse.bodyToMono(UserPayload.class)
                            .flatMap(user -> {
                                saveNewUser(user);
                                validateUser(user.getLinks());
                                return Mono.just(new ResponseEntity<>(user, HttpStatus.OK));
                            }).switchIfEmpty(Mono.error(new NotFoundCustomException("User Payload not found!")));
                }
            }); 

}

Second method:

private Mono validateUser(String uri) {

    webClientBuilder
            .build()
            .post()
            .uri(uri)
            .accept(MediaType.APPLICATION_JSON)
            .contentType(MediaType.APPLICATION_JSON)
            .header("Authorization", settings.getApiToken())
            .retrieve()
            .onStatus(HttpStatus::isError, clientResponse -> clientResponse.bodyToMono(Error.class)
                    .flatMap(error -> Mono.error(new CustomException(clientResponse.statusCode(), error)))
            ).bodyToMono(Void.class);
}

can you help me understand why you give me this warning?

Essentially this POST call gives me results (userPayload), that I reuse in a further POST call and save the data to db.

What's wrong?

public Mono<ResponseEntity> createUser(UserRequest requestPayload) {

   return webClientBuilder
            .build()
            .post()
            .uri(settings.getUrl())
            .accept(MediaType.APPLICATION_JSON)
            .contentType(MediaType.APPLICATION_JSON)
            .header("Authorization", settings.getApiToken())
            .body(BodyInserters.fromValue(requestPayload))
            .exchange()
            .flatMap(clientResponse -> {
                if (clientResponse.statusCode().isError()) {
                    return clientResponse.bodyToMono(Error.class)
                            .flatMap(error -> Mono.error(new CustomException(clientResponse.statusCode(), error)));
                } else {
                    return clientResponse.bodyToMono(UserPayload.class)
                            .flatMap(user -> {
                                saveNewUser(user);
                                validateUser(user.getLinks());
                                return Mono.just(new ResponseEntity<>(user, HttpStatus.OK));
                            }).switchIfEmpty(Mono.error(new NotFoundCustomException("User Payload not found!")));
                }
            }); 

}

Second method:

private Mono validateUser(String uri) {

    webClientBuilder
            .build()
            .post()
            .uri(uri)
            .accept(MediaType.APPLICATION_JSON)
            .contentType(MediaType.APPLICATION_JSON)
            .header("Authorization", settings.getApiToken())
            .retrieve()
            .onStatus(HttpStatus::isError, clientResponse -> clientResponse.bodyToMono(Error.class)
                    .flatMap(error -> Mono.error(new CustomException(clientResponse.statusCode(), error)))
            ).bodyToMono(Void.class);
}

In the end, I solved it like this:

public Mono<ResponseEntity<UserPayload>> createUser(UserRequest requestPayload) {

        return webClientBuilder
                .build()
                .post()
                .uri(settings.getUrl())
                .accept(MediaType.APPLICATION_JSON)
                .contentType(MediaType.APPLICATION_JSON)
                .header("Authorization", settings.getApiToken())
                .body(BodyInserters.fromValue(requestPayload))
                .retrieve()
                .onStatus(HttpStatus::isError, errorResponse -> errorResponse
                        .bodyToMono(Error.class)
                        .flatMap(error -> Mono.error(new CustomException(errorResponse.statusCode(), error))))
                .bodyToMono(UserPayload.class)
                .flatMap(user -> {
                return Mono.fromSupplier(()->repository.save(convertFromPayloadUser(user)))
                        .subscribeOn(Schedulers.boundedElastic())
                        .then(validateUser(user.getLinks()))
                        .then(Mono.just(new ResponseEntity<>(user, HttpStatus.OK)));
            }).switchIfEmpty(Mono.error(new NotFoundCustomException("User Payload not found!")));
    }

second method:

private Mono<Void> validateUser(String uri) {
        return webClientBuilder
                .build()
                .post()
                .uri(uri)
                .accept(MediaType.APPLICATION_JSON)
                .contentType(MediaType.APPLICATION_JSON)
                .header("Authorization", settings.getApiToken())
                .retrieve()
                .onStatus(HttpStatus::isError, clientResponse -> clientResponse.bodyToMono(Error.class)
                        .flatMap(error -> Mono.error(new CustomException(clientResponse.statusCode(), error)))
                ).bodyToMono(Void.class);
    }

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