gpt4 book ai didi

java - 从异步 rest 模板 spring 返回值

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:21:40 25 4
gpt4 key购买 nike

我正在使用 spring 创建一个异步 rest 调用

@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 吗?

最佳答案

您基本上可以做两件事。

  1. 删除 ListenableFutureCallback 并简单地返回 ListenableFuture
  2. 创建一个 DeferredResult 并在 ListenableFutureCallback 中设置它的值。

返回一个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;
}

关于java - 从异步 rest 模板 spring 返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44630914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com