0
        String url = "https://localhost:8080/api/V1/hr/Auth";

        HttpClient client = httpClientFactory.getHttpClient();
        HttpPost request = new HttpPost(url);
        request.addHeader("content-type", "application/json");
        request.addHeader("clientUniqueId", "");
        request.addHeader("language", "English");

        jsonObject.put("UserName", "");
        jsonObject.put("Password", "");

        StringEntity params = new StringEntity(jsonObject.toString());

        request.setEntity(params);
        HttpResponse response;
        response = client.execute(request);
        String responseAsString = EntityUtils.toString(response.getEntity());

I need to replace the httpClient with WebClient I have used webflux dependency. And tried

HttpRequest request1 = (HttpRequest) webClientBuilder
                                         .build()
                                         .post()
                                         .uri(url)
                                         .header("content-type", "application/json")
                                         .header("clientUniqueId", "")
                                         .header("language", "English");

Thanks in advance.

rewolf
  • 5,561
  • 4
  • 40
  • 51
Radhika Mantri
  • 197
  • 1
  • 15

2 Answers2

0

Maybe this links help you springframework.guru

Lordgodgiven
  • 11
  • 1
  • 4
0

The first thing to note is that you should build the webclient once and reuse it if possible. Build it with the most common configuration so that minimal configuration is needed for each request made using it.

The most bare WebClient would be built like this:

WebClient webClient = WebClient.builder()
                               .build();

Now you can use that webClient to make requests.

Mono<String> responseAsString =
        webClient
        .post()
        .uri(uri)
        .contentType(MediaType.APPLICATION_JSON)
        .header("clientUniqueId", "")
        .header("language", "English")
        .bodyValue(jsonObject)
        .exchangeToMono(response -> response.bodyToMono(String.class));

There is a convenience method contentType that will add the header name for you. You can pass the body value using bodyValue which will automatically serialize it to JSON (due to the specified content-type).

exchangeToMono is the method that actual signals the end of the request configuration, and configures what will be done with the response. In this case we are converting the response to a String. More specifically, a Mono of a String. WebClient is "Reactive". This means that it works on the idea that you are building a Stream of operations operating on data. In the stream above, data is sent to an API which converts it to the response. We've signaled that the response must be converted to a String.

We still only have a Mono<String>. If you ran the code, nothing would happen. You MUST subscribe to a stream for something to happen.

responseAsString.subscribe(System.out::println);

would print the output to Stdout.

If you are running this in some main method just for testing, you will need to wait for the subscription, or just block:

System.out.println(responseAsString.block());
rewolf
  • 5,561
  • 4
  • 40
  • 51