简体   繁体   English

从Java中的Promise [JsonNode]获取JsonNode并返回该值

[英]Obtain the JsonNode from a Promise[JsonNode] in Java and return that value

I'm writing a Play 2.3.2 application in Java. 我正在用Java编写Play 2.3.2应用程序。

In my application I make a call to a method of an other module, written in Scala. 在我的应用程序中,我调用了另一个模块的方法,用Scala编写。

This method returns a Json response and I try to obtain that using WS. 此方法返回Json响应,我尝试使用WS获取它。

This is my method implementation: 这是我的方法实现:

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.
    }

But the problem is that the callback returns a Promise, and my method need to return a JsonNode. 但问题是回调返回一个Promise,我的方法需要返回一个JsonNode。

How can I obtain the JsonNode inside the Promise?? 如何在Promise中获取JsonNode? I can'f find any solution to my problem. 我无法找到解决问题的方法。

In Scala I know that I can use the flatMap on a Future[T]. 在Scala我知道我可以在Future [T]上使用flatMap。

Your method performs an asynchronous operation and thus should not return a JsonNode but rather a Promise<JsonNode> . 您的方法执行异步操作,因此不应返回JsonNode ,而应返回Promise<JsonNode>

Conceptually - this makes sense - your method does not immediately fetch the data - rather it dispatches a task relating to the said data that will finish some time in the future. 从概念上讲 - 这是有道理的 - 您的方法不会立即获取数据 - 而是调度与将在未来某个时间完成的所述数据相关的任务。 You can access the response by unwrapping the promise. 您可以通过展开承诺来访问响应。 You can return the Promise<JsonNode> and then call .map on it at the caller site to unwrap the value. 您可以返回Promise<JsonNode> ,然后在调用者站点上调用.map来解包该值。

You can also call .get() on the promise which would force the data to wait for the result (that is - returning jsonPromise.get() but that negates the benefit of using promises to begin with. Note that get() here is different from the get() on holder which is fine since it just returns the promise. 你也可以在promise上调用.get()来强制数据等待结果(即返回jsonPromise.get()但是否定了使用promises开头的好处。注意这里的get()是与持有者的get()不同,因为它只返回了承诺。

Return promise of result instead: 返回结果的承诺:

public static Promise<Result> getPromise() {
    WSRequestHolder url = WS.url("url");
    Promise<Result> promise = url.get().map((r) -> {
        if (r.getStatus() == 200) {
            return ok(r.asJson());
        } else {
            return badRequest("Bad request");
        }
    });
    return promise;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM