针对上个模块提到的ofbiz的wsdl确实不是很规范,那么我们使用axis客户端工具生成的代码肯定不可用,这里我提供了我的客户端调用代码: import java.util.*;import java.net.*;import java.rmi.*;imp
针对上个模块提到的ofbiz的wsdl确实不是很规范,那么我们使用axis客户端工具生成的代码肯定不可用,这里我提供了我的客户端调用代码:
import java.util.*; import java.net.*; import java.rmi.*; import javax.xml.namespace.*; import javax.xml.rpc.*; import org.apache.axis.Message; import org.apache.axis.message.RPCElement; import org.apache.axis.message.RPCParam; import org.apache.axis.message.SOAPEnvelope; import org.apache.axis.client.Call; import org.apache.axis.client.Service; public class AxisClient { /** * 将我们的消息在控制台System.out打印出来 * */ private static Map getResponseParams(Message respMessage) { Map mRet = new Hashtable(); try { SOAPEnvelope resEnv = respMessage.getSOAPEnvelope(); List bodies = resEnv.getBodyElements(); Iterator i = bodies.iterator(); while (i.hasNext()) { Object o = i.next(); if (o instanceof RPCElement) { RPCElement body = (RPCElement) o; List params = null; params = body.getParams(); Iterator p = params.iterator(); while (p.hasNext()) { RPCParam param = (RPCParam) p.next(); mRet.put(param.getName(), param.getValue()); System.out.println("SOAP Client Param - " + param.getName() + "=" + param.getValue()); } } } } catch (org.apache.axis.AxisFault e) { System.out.println("AxisFault"); } catch (org.xml.sax.SAXException e) { System.out.println("SAXException"); } return mRet; } public static void main(String[] args) { String message = ""; Map output; String endpoint; try { //指明我们的服务点 endpoint = "http://192.168.20.32/projectname/control/SOAPService/"; Call call = (Call) new Service().createCall(); call.setTargetEndpointAddress(new URL(endpoint)); //指明要调用的服务名称 call.setOperationName(new QName("findSeniorService", "findSeniorService")); //指明服务的输出输出参数 call.addParameter("userid", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.INOUT); call.addParameter("salt", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN); call.addParameter("aaa", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.OUT); call.addParameter("bbb", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.OUT); call.addParameter("ccc", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.OUT); call.setReturnType(org.apache.axis.Constants.XSD_STRING); //传递参数,发起调用 Object responseWS = call.invoke(new Object[]{"123456789", "aaa"}); System.out.println( "Receiving response: " + (String) responseWS); output = call.getOutputParams(); getResponseParams(call.getMessageContext().getResponseMessage()); } catch (MalformedURLException ex) { message = "error: wrong url"; } catch (ServiceException ex) { message = "error: failed to create the call"; } catch (RemoteException ex) { ex.printStackTrace(); message = "error: failed to invoke WS"; } finally { System.out.println(""); System.out.println(message); } } }
注意上边的endpoint的链接要根据你服务器部署的实际情况来书写。
同时我也提供下xmlspy根据链接生成的数据包,这个不可用:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <SOAP-ENV:Body> <salt xsi:type="xsd:string">String</salt> <userid xsi:type="xsd:string">String</userid> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
看到上边那个生成的不可用的文件主要是没指定服务方法,我们手工改一下,并将我们的参数值奉上:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <SOAP-ENV:Body> <findSeniorService xmlns="http://ofbiz.apache.org/service/"> <salt xsi:type="xsd:string">aaa</salt> <userid xsi:type="xsd:string">2222</userid> </findSeniorService> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
看上边只是指定了我们要给哪个方法传送参数“<findSeniorService xmlns="http://ofbiz.apache.org/service/">”
然后发送soap的信息到webservice接口,我这里的返回值如下:
<?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <ns1:findSeniorServiceResponse xmlns:ns1="http://ofbiz.apache.org/service/"> <aaa xsi:type="xsd:string">test_aaaaa</aaa> <bbb xsi:type="xsd:string">test_bbbbb</bbb> <ccc xsi:type="xsd:string">test_ccccc</ccc> <userid xsi:type="xsd:string">2222</userid> </ns1:findSeniorServiceResponse> </soapenv:Body> </soapenv:Envelope>
这样我就验证了虽然ofbiz提供的webservice的wsdl很不好用,但是那个webservice接口还是可以使用的。只不过只是支持基础数据类型而已。