简体   繁体   English

Play Framework:解析“CompletionStage”(Java8)响应

[英]Play Framework: parse “CompletionStage” (Java8) response

I am totally new to Java 1.8 and to Play Framework. 我是Java 1.8和Play Framework的新手。 Just a simple question: from my application I have to call a REST web service thoroug Play framework and then parse the XML response in order to find some elements of interest. 只是一个简单的问题:从我的应用程序中,我必须调用REST Web服务,然后解析XML响应,以便找到一些感兴趣的元素。 My code, as I found on the guide, looks as follows: 正如我在指南中找到的那样,我的代码如下所示:

WSRequest request = ws.url("http://example.com").setQueryParameter("paramKey", "paramValue");

CompletionStage<Document> documentPromise = request.get()
            .thenApply(WSResponse::asXml);

The question is: how do I parse the "documentPromise" result to find the elements inside the XML? 问题是:如何解析“documentPromise”结果以查找XML中的元素?

Thank you 谢谢

You just have to, instead of use WSResponse::asXml , apply that method yourself and process it as needed. 您只需自己应用该方法,然后根据需要处理它,而不是使用WSResponse::asXml For example, if you just want to return the text of an element, given an id: 例如,如果您只想返回元素的文本,则给定一个id:

// (...)
.thenApply(res -> {
    Document doc = res.asXml();   
    Element e = doc.getElementById("someId");
    return ok(e.getTextContent());
});

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

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