Return Value from async rest template spring

2019-05-17 19:43发布

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 ?

2条回答
Juvenile、少年°
2楼-- · 2019-05-17 20:26

I don't know too much about async calls in Spring but I would imagine that you could return the text that you want through the ResponseBody

It would look like this:

@GetMapping(path = "/testingAsync")
@ResponseBody
public String value() throws ExecutionException, InterruptedException, TimeoutException {
...
...
     @Override
     public void onSuccess(ResponseEntity<User> result) {
         return result.getBody().getName();
     }
...
}

Sorry if this isn't what you are asking about.

查看更多
劫难
3楼-- · 2019-05-17 20:30

There are basically 2 things you can do.

  1. Remove the ListenableFutureCallback and simply return the ListenableFuture
  2. Create a DeferredResult and set the value of that in a ListenableFutureCallback.

Returning a ListenableFuture

@GetMapping(path = "/testingAsync")
public ListenableFuture<ResponseEntity<User>> 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);
    return restTemplate.getForEntity(baseUrl, User.class);
}

Spring MVC will add a ListenableFutureCallback itself to fill a DeferredResult and you will get a User eventually.

Using a DeferredResult

If you want more control on what to return you can use a DeferredResult and set the value yourself.

@GetMapping(path = "/testingAsync")
public DeferredResult<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);
    final DeferredResult<String> result = new DeferredResult<>();
    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());
            result.setResult(result.getBody().getName());
        }

        @Override
        public void onFailure(Throwable ex) {
            result.setErrorResult(ex.getMessage());
        }
    });

    return result;
}
查看更多
登录 后发表回答