我有以下代码块。我得到org.springframework.web.context.request.async.AsyncRequestTimeoutException
catch块没有处理它。谁能告诉我如何处理下面supplyAsync
块抛出的异常?
@org.springframework.scheduling.annotation.Async
public CompletableFuture<ResponseEntity<?>> getTeam(String teamCode) {
CompletableFuture.supplyAsync(() -> {
CricketTeam team;
try {
team = service.getTeamInfo(teamCode);
} catch (Exception ex) {
Error error = new Error(500, ex.getMessage());
return new ResponseEntity<>(error, HttpStatus.OK);
}
return new ResponseEntity<>(team, HttpStatus.OK);
});
}
答案 0 :(得分:3)
也许是这样的(你必须调整类型):
CompletableFuture.supplyAsync(() -> possiblyFailingMethod())
.exceptionally((e) ->
{
log.error("Something went wrong", e);
return "KO";
});