【问题标题】:SOAP webservice client in Play Framework 2.5.1Play Framework 2.5.1 中的 SOAP Web 服务客户端
【发布时间】:2016-04-09 16:29:23
【问题描述】:

我正在尝试使用 Play Webservice API 开发 SOAP webservice 客户端,但我不知道如何做到这一点。我找到了链接:https://www.playframework.com/documentation/2.5.x/JavaWS 但我没有看到任何示例代码,如何使用 WSClient 来使用 SOAP webservice。 任何建议都非常感谢,谢谢。

【问题讨论】:

    标签: web-services soap playframework playframework-2.5


    【解决方案1】:

    使用 Play 使用 SOAP Web 服务的方式基本上有 2 种(为了完整起见,我添加了 2 种)

    1. 使用 Play WS 模块(正如您所尝试的那样) - 因为您将使用 XML,您只需调用:

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

    当然,在您获得 XML 文档后,您可以以任何您想要的方式操作它 - 例如使用 XPath 或其他一些 XML 解析机制:

    String value = XPath.selectText("//value", yourXmlDocument);
    Node node = XPath.selectNode("//node", yourXmlDocument);
    

    1. 使用(一个/您拥有的)SOAP 库 - 您可以在 build.sbt 文件中提供您的库作为依赖项,或者您可以将它放在 Play 项目的类路径中然后使用它。 也看看 scalaxb (http://scalaxb.org/sbt-scalaxb)

    【讨论】:

    • 您好,感谢您的帮助,但我还有一个问题。在您的 1 个示例中,变量 url 应指向 asmx 文件(例如 webservicex.net/globalweather.asmx)或特定的 Web 方法(例如 webservicex.net/globalweather.asmx/GetCitiesByCountry)。我写了以下内容: ws.url("webservicex.net/globalweather.asmx/…", "Ukraine").get().thenApply(WSResponse::asXml) 和 CompletionFuture 已完成,对此网络服务有任何调用,结果文档为空。跨度>
    • URL 应该绝对指向您要使用的资源 - 因此在您的情况下指向 GetCitiesByCountry 操作。但请注意,它们需要 POST 请求 - 所以您必须使用 .post() 而不是 .get()
    • 嗨,我将代码更改为 CompletionStage&lt;Document&gt; responseStage = ws.url("http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry").setQueryParameter("CountryName", "Ukraine").setRequestTimeout(5000).post("content").thenApply(WSResponse::asXml);ws.url("http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry").setRequestTimeout(5000).post("CountryName=Ukraine").thenApply(WSResponse::asXml); 现在 Play 正在抛出 `org.xml.sax.SAXParseException;行号:1;列号:1;序言中不允许有内容。`
    • 因为这是一些与 xml 相关的错误,我正在尝试生成WSResponse 对象(删除thenApply),现在作为响应我得到NettyResponse { statusCode=500 body=System.InvalidOperationException: Request format is invalid: text/plain.}text/xmlapplication/soap+xml 的回复类似
    • 这可能是因为 SOAP 比其他 WS 更复杂,您必须在 HTTP 请求上使用 SOAPAction 标头指定被调用的方法。一般来说,我建议不要编写自己的肥皂客户端,因为有开源可靠的库。
    猜你喜欢
    • 2011-10-11
    • 2010-12-23
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 2018-03-28
    相关资源
    最近更新 更多