I am creating a async rest call using spring
@GetMapping(path = "/testingAsync")
public String value() throws ExecutionException, InterruptedException, TimeoutException {
AsyncRestTemplate restTemplate = new AsyncRestTemplate();
String baseUrl = "https://api.github.com/users/XXX";
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
String value = "";
HttpEntity entity = new HttpEntity("parameters", requestHeaders);
ListenableFuture<ResponseEntity<User>> futureEntity = restTemplate.getForEntity(baseUrl, User.class);
futureEntity.addCallback(new ListenableFutureCallback<ResponseEntity<User>>() {
@Override
public void onSuccess(ResponseEntity<User> result) {
System.out.println(result.getBody().getName());
// instead of this how can i return the value to the user ?
}
@Override
public void onFailure(Throwable ex) {
}
});
return "DONE"; // instead of done i want to return value to the user comming from the rest call
}
And is there any way i can convert ListenableFuture to use CompletableFuture that is used in java 8 ?
futureEntity
instead ofDONE
, Spring MVC will do the rest. Or create aDefferedResult
and set the value of that result in theonSuccess
and return theDeferredresult
from your method.