[英]CompletableFuture.get() outputs rather status than response body
I have the following scenario: I set up a client which sends an aynchronous HTTP reuqest to the server.我有以下场景:我设置了一个客户端,它向服务器发送一个异步的 HTTP reuqest。 The client receives a CompletableFuture.
客户端收到一个 CompletableFuture。 So far evertything works fine.
到目前为止一切正常。 However, I cannot access the response of the request I am sending.
但是,我无法访问我发送的请求的响应。
completableFuture.get().body()
rather contains the status of the request.而是包含请求的状态。 In more detail, I am getting
更详细地说,我得到
{"cancelled":false,"done":true,"completedExceptionally":false,"numberOfDependents":0}
How can I get the acutal result?我怎样才能得到实际的结果?
Here is my code...这是我的代码...
Rest Controller Rest Controller
@RestController
public class WorkerJController {
@Autowired
private WorkerJService service;
@GetMapping(value = "/JobList", produces = MediaType.APPLICATION_JSON_VALUE)
public CompletableFuture<ResponseEntity> getJobListFunction() throws JsonProcessingException, InterruptedException {
return CompletableFuture.completedFuture(service.getJobListFunction()).thenApply(ResponseEntity::ok);
}
}
Service服务
@Service
public class WorkerJService {
public static ArrayList<someThing> randomList = new ArrayList<>();
@Async
public CompletableFuture<String> getJobListFunction() throws JsonProcessingException, InterruptedException {
randomList.add(new someThing("abc", "dfe"));
ObjectMapper mapper = new ObjectMapper();
TimeUnit.SECONDS.sleep(5);
return CompletableFuture.completedFuture(mapper.writeValueAsString(jobList));
}
}
Async Config异步配置
@Configuration
@EnableAsync
public class AsyncConfig {
@Bean(name = "taskExecutor")
public Executor taskExecutor(){
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(3);
executor.setMaxPoolSize(3);
executor.setQueueCapacity(100);
executor.setThreadNamePrefix("AsynchThread-");
executor.initialize();
return executor;
I send the HTTP request as follows:我发送 HTTP 请求如下:
HttpClient client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.followRedirects(HttpClient.Redirect.NORMAL)
.connectTimeout(Duration.ofSeconds(20))
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8080/JobList"))
.timeout(Duration.ofMinutes(2))
.GET()
.build();
CompletableFuture<HttpResponse<String>> cf = client
.sendAsync(request, HttpResponse.BodyHandlers.ofString());
CompletableFuture.completedFuture(service.getJobListFunction())
wraps the CompletableFuture
returned by service.getJobListFunction()
into another CompletableFuture
. CompletableFuture.completedFuture(service.getJobListFunction())
将service.getJobListFunction()
返回的CompletableFuture
包装到另一个CompletableFuture
中。
Just chain the response from the service directly with the thenApply()
instead:只需将来自服务的响应直接与
thenApply()
:
@GetMapping(value = "/JobList", produces = MediaType.APPLICATION_JSON_VALUE)
public CompletableFuture<ResponseEntity> getJobListFunction() throws JsonProcessingException, InterruptedException {
return service.getJobListFunction().thenApply(ResponseEntity::ok);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.