I want to use Java Http Client: java.net.http.HttpClient
in asynchronous way.
When I receive http response (any http code) or timeout I want to execute some custom Java code. I struggle to complete the body of error handling.
CompletableFuture<HttpResponse<String>> response =
httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApplyAsync(body -> {
System.out.println("Request finished");
return body;
})
.exceptionallyAsync(exception -> {
// WHAT TO RETURN HERE ?
});
The method: exceptionallyAsync
returns: CompletableFuture<T>
, but I do not know how to complete this method.
Can you please help me to finish this method? I cannot find example in GitHub or Google.