Home > other >  to consume POST request client method using WEBCLIENT
to consume POST request client method using WEBCLIENT

Time:10-26

I'm new to the WebClient field, but I'm running into the following problem. This is my client method, which you have seen should insert the values that I have sent to the parameter to the database, but I cannot insert it.

 @Override
    public Mono<BulkSmsRequestResource> sendSms(BulkSmsRequestResource request) {
        Mono<BulkSmsRequestResource> bulkSmsRequestResourceMono = webClientBuilder.build()
                .post()
                .uri(url1)
                .body(Mono.just(request), BulkSmsRequestResource.class)
                .retrieve()
                .bodyToMono(BulkSmsRequestResource.class);
        return bulkSmsRequestResourceMono;

    }

The code I wrote here is my controller area.

@PostMapping("/send")
public ResponseEntity<? extends ResponseResource> sendSms(@RequestBody BulkSmsRequestResource request) {
    if (request == null) {
        return new ResponseEntity<>(
                new ErrorResponseResource(
                        "Transaction failed successfully!",
                        400),
                HttpStatus.BAD_REQUEST);
    }
    this.smsSendService.sendSms(request);
    return new ResponseEntity<>(new SuccessResponseResource("Transaction done successfully", 200), HttpStatus.OK);

}

When I run my method, I can't add the values to the database on the server side, but I can get the values from there using the following method using the client method.

@Override
public Flux<BulkSmsResponseDto> findAll() {
    Flux<BulkSmsResponseDto> bulkSmsResponseDtoFlux = webClientBuilder.build()
            .get()
            .uri(url2)
            .retrieve()
            .bodyToFlux(BulkSmsResponseDto.class);
    return bulkSmsResponseDtoFlux;
}

CodePudding user response:

In order to trigger the WebClient reactive request, you must subscribe to the lattery to trigger the HTTP call:

service.sendSms(request)
    .subscribe(System.out::println);

Otherwise, you will have to chain the Mono<?> to the controller method result so that the underlying infrastructure (Spring in this case) will know how to handle it (subscribing to it at alter point resulting in the HTTP call being invoked).

So in order to have the WebClient request triggered upon your controller endpoint invocation, you have to map its return result to the proper ResponseEntity then return the result Mono:

@PostMapping("/send")
public Mono<ResponseEntity<? extends ResponseResource>> sendSms(@RequestBody BulkSmsRequestResource request) {
    if (request == null) {
        return Mono.just(new ResponseEntity<>(
                new ErrorResponseResource(
                        "Transaction failed successfully!",
                        400),
                HttpStatus.BAD_REQUEST));
    }
    return this.smsSendService.sendSms(request)
        .map(result -> new ResponseEntity<>(new SuccessResponseResource("Transaction done successfully", 200), HttpStatus.OK));
}

Design side note: Maybe you need to update the response with proper HTTP code (201) along with a reference to the created resource.

  • Related