ofbiz soap webservice
??
OFBIZ webservice简介? Opentaps(OFBiz 9.04之后)中webservice用的是AXIS2,最开始自己在网上搜了好多资料,自己拿回来测试,发现都不对。后自己再找了下AXIS的资料说,那种报错很有可能是由于两个版本不对引起的,所以就决定看看OFBiz里面用的是哪个版本,当时我彻底无语了,里面两个版本的包竟然都有,真不知道是什么意思。但是我认为应该是AXIS2,OFBiz这么与时俱进的东西,应该不太可能用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测试服务,我们添加一个service,export="true"加上去,最后变成: ? 方法java代码为: ? <!--[if !supportLists]-->② <!--[endif]-->:现在只是发布了,但是我们必须要知道怎样请求才能得到这个服务,ofbiz提供了一个event来处理它,就是<handler name="soap" type="request" class="org.ofbiz.webapp.event.SOAPEventHandler"/>,要使用它,你必须把这个定义在你的controller.xml文件中,当然,如果你已经引入了<include location="component://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 List<ClientGenericValue> valueList; private String om; private String serviceName; ? public ClientUtil(String endPoint,String serviceName,String om, List<ClientGenericValue> 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",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/"; List<ClientGenericValue> 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 List<ClientGenericValue> getTestData(){ List<ClientGenericValue> valueList = new ArrayList<ClientGenericValue>(); ClientGenericValue value1 = new ClientGenericValue(); value1.setType("std-String"); value1.setKey("userid"); value1.setValue("22222"); ? 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; } } 转自:http://www.cnblogs.com/Ivan-j2ee/archive/2012/12/12/2813920.html(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |