ofbiz迄今为止,最新的版本为11.04,而在9.04等版本中,使用的是axis1 ,在11.04中,使用的是axis2.因此调用Webservice客户端的方法也不一样.
今天尝试着调用了ofbiz11.04的Webservice:
在service.xml中,将service的export设置为true,暴露成外部的Webservice.以我的SendiEmail服务为例
在地址栏中输入:http://127.0.0.1:8080/webtools/control/SOAPService/SendEmail?wsdl
<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ofbiz.apache.org/service/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ofbiz.apache.org/service/">
- <wsdl:types>
- <xsd:schema targetNamespace="http://ofbiz.apache.org/service/">
- <xsd:element name="std-String">
- <xsd:complexType>
<xsd:attribute name="value" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
- <xsd:element name="std-Integer">
- <xsd:complexType>
<xsd:attribute name="value" type="xsd:integer" use="required" />
</xsd:complexType>
</xsd:element>
- <xsd:element name="std-Long">
- <xsd:complexType>
<xsd:attribute name="value" type="xsd:long" use="required" />
</xsd:complexType>
</xsd:element>
- <xsd:element name="std-Float">
- <xsd:complexType>
<xsd:attribute name="value" type="xsd:float" use="required" />
</xsd:complexType>
</xsd:element>
- <xsd:element name="std-Double">
- <xsd:complexType>
<xsd:attribute name="value" type="xsd:double" use="required" />
</xsd:complexType>
</xsd:element>
- <xsd:element name="std-Boolean">
- <xsd:complexType>
<xsd:attribute name="value" type="xsd:boolean" use="required" />
</xsd:complexType>
</xsd:element>
- <xsd:element name="std-Locale">
- <xsd:complexType>
<xsd:attribute name="value" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
+ <xsd:element name="sql-Timestamp">
.........
如果访问之后代码如这样,就表示设置成功.
加入axis2的jar包,这个不说明了,自己百度.
调用代码如下
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
public class TestUtil {
private static OMFactory fac=OMAbstractFactory.getOMFactory();
private static OMNamespace omNs;
public static void main(String[] args) throws AxisFault {
ServiceClient sc = new ServiceClient();
Options opts = new Options();
opts.setTo(new EndpointReference(
"http://localhost:8080/webtools/control/SOAPService"));
opts.setAction("SendEmail");
sc.setOptions(opts);
OMElement res = sc.sendReceive(createPayLoad());
System.out.println(res);
}
private static OMElement createPayLoad() {
OMElement SendEmail = null;
try {
createSendMultiEmail = fac.createOMElement("SendEmail", omNs);
OMElement mapMap = fac.createOMElement("map-Map", omNs);
createSendMultiEmail.addChild(mapMap);
mapMap.addChild(createMapEntry("login.username", "admin"));
mapMap.addChild(createMapEntry("login.password", "ofbiz"));
mapMap.addChild(createMapEntry("emailList", "111@126.com"));
mapMap.addChild(createMapEntry("emailSubject", "123"));
mapMap.addChild(createMapEntry("emailContent", "456"));
mapMap.addChild(createMapEntry("sentDate", "2012-03-11"));
mapMap.addChild(createMapEntry("sentDateHour", "12"));
mapMap.addChild(createMapEntry("sentDateMin", "07"));
} catch (Exception e) {
e.printStackTrace();
}
return SendEmail;
}
private static OMElement createMapEntry(String key, String val) {
OMElement mapEntry = fac.createOMElement("map-Entry", omNs);
// create the key
OMElement mapKey = fac.createOMElement("map-Key", omNs);
OMElement keyElement = fac.createOMElement("std-String", omNs);
OMAttribute keyAttribute = fac.createOMAttribute("value", null, key);
mapKey.addChild(keyElement);
keyElement.addAttribute(keyAttribute);
// create the value
OMElement mapValue = fac.createOMElement("map-Value", omNs);
OMElement valElement = fac.createOMElement("std-String", omNs);
OMAttribute valAttribute = fac.createOMAttribute("value", null, val);
mapValue.addChild(valElement);
valElement.addAttribute(valAttribute);
// attach to map-Entry
mapEntry.addChild(mapKey);
mapEntry.addChild(mapValue);
return mapEntry;
}
}
测试结果如下,在控制台代码为:
Sent Hello, got : <SendEmailResponse xmlns="http://ofbiz.apache.org/service/"><map-Map>
<map-Entry>
<map-Key>
<std-String value="responseMessage"></std-String>
</map-Key>
<map-Value>
<std-String value="success"></std-String>
</map-Value>
</map-Entry>
</map-Map></SendEmailResponse>
来源:oschina
链接:https://my.oschina.net/u/99877/blog/48594