在Play 1.x框架中使用SOAP Web服务

时间:2013-03-27 07:38:12

标签: java web-services soap playframework

我只是想知道有没有办法在Play框架内部使用SOAP Web服务,特别是版本1.x.x

由于

2 个答案:

答案 0 :(得分:2)

其他答案当然是有效的,但Play附带了一些方便的课程。您需要手动解析响应。 从WS课开始。它可用于发布/获取或提供各种服务。我将它用于SOAP请求和REST调用。

示例:

HttpResponse httpResponse = null;
String username = "";
String password = "";
String url = "";
String postBody = "";

try {
    httpResponse = WS.url(url)
        .authenticate(username, password)
        .setHeader("Content-Type", "text/xml; charset=UTF-8")
        .setHeader("SOAPAction", "")
        .body(postBody).post();

    Document document = httpResponse.getXml();
    String value = XPath.selectText("//value", document);
    Node node = XPath.selectNode("//node", document);

    // Do things with the nodes, value and so on

} catch (Exception e) {
    Logger.error("Do something with the connection error: %s", e);
}

如您所见,我使用XPath类来解析返回的Document。它提供了各种有用的方法来遍历文档。

答案 1 :(得分:1)

使用play作为SOAP使用者应该很简单:包括您选择的soap库,从wsdl生成存根,调用端点。另一种选择是调用URL并使用Xpath解析其信封。