Ofbiz默认支持提供Soap的WebService服务。需要在Service的定义文件中,增加属性export=true.
本文将以getAllCategories服务为例,该服务是在Product模块下的servcie-maint.xml中。
首先增加export="true",如下:
<service name="getAllCategories" engine="simple" export="true"
location="component://product/minilang/product/catalog/CatalogServices.xml" invoke="getAllCategories">
<description>Get all categories of a category </description>
<attribute name="topCategory" type="String" mode="IN" optional="true"/>
<attribute name="categories" type="java.util.List" mode="OUT" optional="true"/>
</service>
然后重启ofbiz服务,此时就可以查看该服务的WSDL了,路径为:http://192.168.122.9:8080/webtools/control/SOAPService/getAllCategories?wsdl.可以通过第三方工具SoapUI进行测试。测试如下:(若需要在Ajax中调用,可直接使用http://192.168.122.9:8080/webtools/control/httpService?serviceName=getAllCategories&serviceContext=***,serviceContext为参数的Map格式序列化)
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://ofbiz.apache.org/service/">
<soapenv:Header/>
<soapenv:Body>
<ser:getAllCategories>
<map-Map>
<ser:map-Entry>
<ser:map-Key>
<ser:std-String value="login.username"/>
</ser:map-Key>
<ser:map-Value>
<ser:std-String value="admin"/>
</ser:map-Value>
<ser:map-Key>
<ser:std-String value="login.password"/>
</ser:map-Key>
<ser:map-Value>
<ser:std-String value="ofbiz"/>
</ser:map-Value>
<ser:map-Key>
<ser:std-String value="topCategory"/>
</ser:map-Key>
<ser:map-Value>
<ser:std-String value=""/>
</ser:map-Value>
</ser:map-Entry>
</map-Map>
</ser:getAllCategories>
</soapenv:Body>
</soapenv:Envelope>
可以看到,代码中除了服务所必须的参数topCategory 外,还额外增加了用户的登录账号和密码。参数是以Map的Key-Value对的形式传入的。传出的结果也是如此。
经我测试,C#代码直接引入WebService调用未能成功。此处采用HttpWebRequest类Post发送Xml数据的形式实现:
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://192.168.122.9:8080/webtools/control/SOAPService");
webRequest.Headers.Add("SOAPAction", "https://192.168.122.9:8443/webtools/control/SOAPService");
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
XmlDocument soapEnvelopeDocument = new XmlDocument();
soapEnvelopeDocument.LoadXml(@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ser=""http://ofbiz.apache.org/service/""><soapenv:Header/><soapenv:Body><ser:getAllCategories><map-Map><ser:map-Entry><ser:map-Key><ser:std-String value=""login.username""/></ser:map-Key><ser:map-Value><ser:std-String value=""admin""/></ser:map-Value><ser:map-Key><ser:std-String value=""login.password""/></ser:map-Key><ser:map-Value><ser:std-String value=""ofbiz""/></ser:map-Value><ser:map-Key><ser:std-String value=""topCategory""/></ser:map-Key><ser:map-Value><ser:std-String value=""""/></ser:map-Value></ser:map-Entry></map-Map></ser:getAllCategories></soapenv:Body></soapenv:Envelope>");
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeDocument.Save(stream);
}
using (var webResponse = webRequest.GetResponse())
{
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
var soapResult = rd.ReadToEnd();
}
}