问题
I just want to know is there any way to consume a SOAP web-service inside Play framework specifically version 1.x.x
Thanks
回答1:
The other answers are of course valid, but Play comes with a few handy classes for this kind of stuff. You will need to parse the response by hand though.
Start with the WS
class. It can be used to post/get or whatever with all kinds of services. I use it for SOAP requests and REST calls for instance.
Example:
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);
}
As you can see, I use the XPath
class to parse the returned Document
. It offers all kinds of useful methods to traverse the Document.
回答2:
Using play as a SOAP consumer should be straightforward: include the soap library of your choice, generate the stubs from the wsdl, call the endpoint. Another option is to call the URL and use Xpath to parse its envelope.
来源:https://stackoverflow.com/questions/15653821/consume-soap-web-service-in-play-1-x-framework