【发布时间】:2016-11-17 16:44:49
【问题描述】:
在我们的控制器类中,我们联系另一个服务来获取一些数据:
Future<JsonNode> futureSite = someClient.getSite(siteId, queryParams);
return FutureConverters.toJava(futureSite).thenApplyAsync((siteJson) -> {
Site site = Json.fromJson(siteJson, Site.class);
try {
return function.apply(site);
} catch (RequestException e) {
return e.result;
}
}).exceptionally(throwable -> {
if(throwable instanceof OurClientException) {
if(((OurClientException) throwable).httpStatusCode == 404) {
return entityNotFound("Site", siteId);
}
}
return null;
});
我们注意到在单元测试中设置的上下文(我们使用 scalatest-play)在我们进行异步调用 (FutureConverters.toJava(futureSite).thenApplyAsync((siteJson) 后丢失并变为空,因为 t 在单独的线程上。
这会导致控制器代码出现问题,我们使用上述函数... request() 现在会抛出运行时异常,说明没有可用的上下文。
我们如何保存上下文?
【问题讨论】:
标签: scala playframework scalatest playframework-2.5