建设网站加盟,深圳家装互联网网站,装修之家,做网站要考虑哪些因素OFBIZ webservice简介 Opentaps(OFBiz 9.04之后)中webservice用的是AXIS2#xff0c;最开始自己在网上搜了好多资料#xff0c;自己拿回来测试#xff0c;发现都不对。后自己再找了下AXIS的资料说#xff0c;那种报错很有可能是由于两个版本不对引起的#xff0c;所以就决… OFBIZ webservice简介 Opentaps(OFBiz 9.04之后)中webservice用的是AXIS2最开始自己在网上搜了好多资料自己拿回来测试发现都不对。后自己再找了下AXIS的资料说那种报错很有可能是由于两个版本不对引起的所以就决定看看OFBiz里面用的是哪个版本当时我彻底无语了里面两个版本的包竟然都有真不知道是什么意思。但是我认为应该是AXIS2OFBiz这么与时俱进的东西应该不太可能用06年就不更新的架包。 废话少说直接说开发步骤吧 一在项目中引入AXIS2由于AXIS2的依赖包好几个客户端应该不需要那么多但是以防万一我们把AXIS2下面lib目录的所有jar包一并加入到开发工程的classpath下。 二开发webservice必须知道wsdl才能比较好的。首先我们在OFBiz中打开一个service让它能被发不成webservice。这个非常简单在OFBiz中你只要在service定义的xml中把要发布的service添加一个属性export”true”重启服务就能看到。下面以一个实例来说明 !--[if !supportLists]--① !--[endif]--我们找到application/order/servicedef/services.xml文件打开找到最后一个service这里有个自带的SOAP测试服务我们添加一个serviceexporttrue加上去最后变成 方法java代码为 !--[if !supportLists]--② !--[endif]--现在只是发布了但是我们必须要知道怎样请求才能得到这个服务ofbiz提供了一个event来处理它就是handler namesoap typerequest classorg.ofbiz.webapp.event.SOAPEventHandler/要使用它你必须把这个定义在你的controller.xml文件中当然如果你已经引入了include locationcomponent://common/webcommon/WEB-INF/common-controller.xml/那么就不需要了这个里面已经定义好了。直接使用就行了。 !--[if !supportLists]--③ !--[endif]--重启服务在浏览器中输入 http://localhost:8080/ordermgr/control/SOAPServices/testSoap?wsdl如果你看到了你刚才发布的服务说明已经成功。如下图 三客户端编写。 客户端代码编写最主要是要知道服务端想要的是什么东西首先我们的服务类的定义我们可以看到是需要一个输入输出一个输入三个输出的也就是两个进两个出。在wsdl中我们可以看到 说明我们需要传入的是一个map-Map形式的Model,并且salt和userid是必须的。由于我们没有设置验证信息所以login.username和login.password是可以不需要的。直接上代码比较好一些。 在测试的时候我仅仅弄了一个很简单的例子由于axiom比较复杂还要详细研究下。类说明ClientGenricValue封装的是要传入数据的类型键key和值。ClientUtil package com.wx; public class ClientGenericValue { private String type; private String key; private String value; public String getType() { return type; } public void setType(String type) { this.type type; } public String getKey() { return key; } public void setKey(String key) { this.key key; } public String getValue() { return value; } public void setValue(String value) { this.value value; } } package com.wx; import java.util.List; import org.apache.axiom.om.OMAbstractFactory; 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 ClientUtil { private static OMFactory factory OMAbstractFactory.getOMFactory(); private String endPoint; private ListClientGenericValue valueList; private String om; private String serviceName; public ClientUtil(String endPoint, String serviceName , String om, ListClientGenericValue valueList) { this.endPoint endPoint; this.serviceName serviceName; this.om om; this.valueList valueList; } public ServiceClient createClient() throws AxisFault { ServiceClient client new ServiceClient(); Options options new Options(); EndpointReference targetERP new EndpointReference(endPoint); //定义目的EndpointReference就是服务地址 options.setTo(targetERP); options.setTimeOutInMilliSeconds(400000); //定义超时这里可以不定义 client.setOptions(options); return client; } public OMElement send() throws AxisFault{ OMNamespace ns factory.createOMNamespace(http://ofbiz.apache.org/service/, serviceName); OMElement root factory.createOMElement(serviceName, ns); OMElement data factory.createOMElement(map-HashMap, null); root.addChild(data); for(ClientGenericValue value : valueList){ OMElement mapKey factory.createOMElement(map-Entry, null); OMElement keyElement factory.createOMElement(map-Key, null); OMElement keyValue factory.createOMElement(std-String, null); keyValue.addAttribute(factory.createOMAttribute(value, null, value.getKey())); keyElement.addChild(keyValue); OMElement valueElement factory.createOMElement(map-Value, null); OMElement valueValue factory.createOMElement(value.getType(), null); valueValue.addAttribute(factory.createOMAttribute(value, null, value.getValue())); valueElement.addChild(valueValue); mapKey.addChild(keyElement); mapKey.addChild(valueElement); data.addChild(mapKey); } System.out.println(root); OMElement result createClient().sendReceive(root); return result; } public String getEndPoint() { return endPoint; } public void setEndPoint(String endPoint) { this.endPoint endPoint; } } package com.wx; import java.util.ArrayList; import java.util.List; import org.apache.axiom.om.OMElement; import org.apache.axis2.AxisFault; public class ClientTest { /** * param args * throws AxisFault */ public static void main(String[] args) { String endPoint http://localhost:8080/ordermgr/control/SOAPServices/testSoap; String serviceName testSoap; String om http://ofbiz.apache.org/service/; ListClientGenericValue valueList getTestData(); ClientUtil cu new ClientUtil(endPoint, serviceName, om, valueList); try { OMElement result cu.send(); System.out.println(Sent Hello, got : result.toString()); } catch (AxisFault e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static ListClientGenericValue getTestData(){ ListClientGenericValue valueList new ArrayListClientGenericValue(); ClientGenericValue value1 new ClientGenericValue(); value1.setType(std-String); value1.setKey(userid); value1.setValue(11111); ClientGenericValue value2 new ClientGenericValue(); value2.setType(std-String); value2.setKey(salt); value2.setValue(The power of OFBiz); // ClientGenericValue value3 new ClientGenericValue(); // value3.setType(eeval-OrderItemTypeAttr); // value3.setKey(testing); // value3.setValue(org.ofbiz.entity.GenericValue); //这个可以不用填写的 valueList.add(value1); valueList.add(value2); // valueList.add(value3); return valueList; } } 转载于:https://www.cnblogs.com/Ivan-j2ee/archive/2012/12/12/2813920.html