从异步休息模板spring返回值

时间:2017-06-19 12:43:50

标签: java spring rest asynchronous rest-client

我正在使用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 
}

有什么方法可以将ListenableFuture转换为使用java 8中使用的CompletableFuture?

2 个答案:

答案 0 :(得分:7)

你基本上可以做两件事。

  1. 移除ListenableFutureCallback,然后返回ListenableFuture
  2. 创建DeferredResult并在ListenableFutureCallback
  3. 中设置该值

    返回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会添加一个ListenableFutureCallback来填充DeferredResult,最终会得到User

    使用DeferredResult

    如果您想要更多地控制要返回的内容,可以使用DeferredResult并自行设置值。

    @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;
    }
    

答案 1 :(得分:0)

我不太了解Spring中的异步调用,但我想你可以通过 ResponseBody

返回你想要的文本

看起来像这样:

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

很抱歉,如果这不是您的要求。