【发布时间】:2014-12-18 20:28:02
【问题描述】:
我正在用 Java 编写一个 Play 2.3.2 应用程序。
在我的应用程序中,我调用了另一个模块的方法,用 Scala 编写。
这个方法返回一个 Json 响应,我尝试使用 WS 来获取它。
这是我的方法实现:
public static JsonNode getCorrelationData() {
WSRequestHolder holder = WS.url(ConfigFactory.load().getString("host") + "/recommendation/correlation");
Promise<JsonNode> jsonPromise = holder.get().map(
new Function<WSResponse, JsonNode>() {
public JsonNode apply(WSResponse response) {
if (response.getStatus() != 200) {
Logger.error("Error on get correlation data");
Logger.error("Response status code: " + response.getStatus());
Logger.error("Response status text: " + response.getStatusText());
}
return response.asJson();
}
});
//here I want to obtain the JsonNode inside the jsonPromise object, and return it.
}
但问题是回调返回一个Promise,而我的方法需要返回一个JsonNode。
如何获取 Promise 中的 JsonNode?我找不到任何解决问题的方法。
在 Scala 中,我知道我可以在 Future[T] 上使用 flatMap。
【问题讨论】:
标签: java playframework callback promise