Spring boot WebClient sends multiple POST requests instead of one [closed]

I am using WebClient (Spring boot library) for sending client requests (POST) to the server. The WebClient sometimes unexpectedly sends multiple (upto 20) POST requests, even though I intended to send only one. As a result, a single record is stored multiple times on the server’s database, which is absurd.
Here is the code used for sending requests to the server:

   WebClient.builder()
            .baseUrl(baseUrl)
            .defaultHeader("Authorization","Bearer "+jwt)
            .codecs(codecs -> codecs
                    .defaultCodecs()
                    .maxInMemorySize(MB))
            .build().post().uri("/some/url/")
                .body(Mono.just(object),MyObject.class)
                .retrieve()
                .onStatus(HttpStatus::isError, clientResponse -> Mono.error(new      ClientRequestException ("ERROR:" + clientResponse.statusCode())))
                .bodyToMono(new ParameterizedTypeReference<ServiceResponse<ReturnedObject>>() {})
                .retryWhen(Retry.backoff(2, Duration.of(2, 
                   ChronoUnit.SECONDS))
                .onRetryExhaustedThrow((retryBackoffSpec, 
                   retrySignal) ->
                  new ClientRequestException("FAILED TO CONNECT")))
                .onErrorResume(ClientRequestException.class, ex -> 
                 { Platform.runLater(()-> 
                   ShowMessage.networkDown(table));
                   return Mono.empty();
                }).doOnSuccess(p->{ 
                   // show success message!!
                })
                .subscribe(); 

I tried to stop storing multiple same records in the database by setting the datetime column unique in the database’s table; however, this isn’t the right way to solve this problem.

  • 1

    The code is poorly formatted so hard to read, but I see a retry in there. Do you think that might be the problem?

    – 

  • So I guess this is cargo cult programming, you copied some code from somewhere without understanding what it really does.

    – 

  • I don’t think the retry is the problem. If it was, then there would be only 2 requests sent at the same time but it’s more than 2 requests. @jewelsea The code is mine and I developed the whole app, so your guess is wrong.

    – 

  • Thanks for the reply. As you understand the code provided and are sure it is not the cause of your issue, then likely the cause lies elsewhere (i.e. in code not provided). Assistance would probably require a minimal reproducible example to help debug the issue.

    – 




Leave a Comment