Naanavanalla
Naanavanalla

Reputation: 1522

Spring webclient throwing block not supported exception

I am trying to make an HTTP call from spring-boot. The call is working fine in post man, below is the curl version of the call,

curl --location --request POST 'https://sampletest.com:8811/rest/oauth/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Accept: application/json' \
--data-urlencode 'client_id=126763****ghsei99****' \
--data-urlencode 'client_secret=126763****ghsei99****' \
--data-urlencode 'param1=pppp' \
--data-urlencode 'param2=pppp'

But the same call I am trying to make using WebClient.

WebClient webClient = WebClient.builder().baseUrl("https://sampletest.com:8811")
                    .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE).build();

            MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
            formData.add("client_id","126763****ghsei99****");
            formData.add("client_secret", "126763****ghsei99****");
            formData.add("param1","pppp");
            formData.add("param2","pppp");

            AuthenticationResponseBean authenticationResponseBean = webClient.post().uri("/rest/oauth/token").body(BodyInserters.fromFormData(formData))
                    .retrieve().bodyToFlux(AuthenticationResponseBean.class).blockLast();

But, the app is throwing the exception,

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

What is going wrong here? I am very new to reactive-programming and the google search for the problem indicates why is the block needed?

Thank You in advance.

Upvotes: 1

Views: 696

Answers (1)

Colon
Colon

Reputation: 415

As per my understanding, blocking actually blocks the thread. From the ProyectReactor docs:

  • "block indefinitely until the upstream signals its last value or completes".

So you dont need it if you dont need blocking, or you dont need to wait for the last element. So just subscribing could work, depending on your needs.

Upvotes: 1

Related Questions