I have a my WebClient class as per below:
public class WebClientSample {
public static void main(String[] args) throws InterruptedException {
System.out.println(mySimpleTestMethod());
}
public static String mySimpleTestMethod() throws InterruptedException {
String uri = "http://localhost:8080/some/cool/api/here";
WebClient webClient = WebClient.create(uri);
Mono<String> result = webClient
.get()
.headers(headers -> headers.setBasicAuth("admin", "secret"))
.retrieve()
.bodyToMono(String.class);
String responseStr = result.subscribe(response -> System.out.println(response)).toString();
Thread.sleep(1000);
return responseStr;
}
}
After execution I get this on my console:
{"some":{"cool":json,"response":{"foo":"bar",...}}}
reactor.core.publisher.LambdaMonoSubscriber@60b71e8f
Question: If I comment Thread.sleep(1000); then I dont get any response. Why do I need to wait for the response?