简体   繁体   中英

to consume POST request client method using WEBCLIENT

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;
}

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.

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