ofbiz的webservice接口提供(3)-不规范的wsdl的客户端访问代码

来源:互联网 发布:电工模拟仿真软件 编辑:程序博客网 时间:2024/06/04 19:34

 

针对上个模块提到的ofbiz的wsdl确实不是很规范,那么我们使用axis客户端工具生成的代码肯定不可用,这里我提供了我的客户端调用代码:

[java] view plaincopy
  1. import java.util.*;
  2. import java.net.*;
  3. import java.rmi.*;
  4. import javax.xml.namespace.*;
  5. import javax.xml.rpc.*;
  6. import org.apache.axis.Message;
  7. import org.apache.axis.message.RPCElement;
  8. import org.apache.axis.message.RPCParam;
  9. import org.apache.axis.message.SOAPEnvelope;
  10. import org.apache.axis.client.Call;
  11. import org.apache.axis.client.Service;
  12. publicclass AxisClient {
  13. /**
  14. * 将我们的消息在控制台System.out打印出来
  15. * */
  16. privatestatic Map getResponseParams(Message respMessage) {
  17. Map mRet =new Hashtable();
  18. try {
  19. SOAPEnvelope resEnv = respMessage.getSOAPEnvelope();
  20. List bodies = resEnv.getBodyElements();
  21. Iterator i = bodies.iterator();
  22. while (i.hasNext()) {
  23. Object o = i.next();
  24. if (o instanceof RPCElement) {
  25. RPCElement body = (RPCElement) o;
  26. List params =null;
  27. params = body.getParams();
  28. Iterator p = params.iterator();
  29. while (p.hasNext()) {
  30. RPCParam param = (RPCParam) p.next();
  31. mRet.put(param.getName(), param.getValue());
  32. System.out.println("SOAP Client Param - " + param.getName() +"=" + param.getValue());
  33. }
  34. }
  35. }
  36. }catch (org.apache.axis.AxisFault e) {
  37. System.out.println("AxisFault");
  38. }catch (org.xml.sax.SAXException e) {
  39. System.out.println("SAXException");
  40. }
  41. return mRet;
  42. }
  43. publicstaticvoid main(String[] args) {
  44. String message ="";
  45. Map output;
  46. String endpoint;
  47. try {
  48. //指明我们的服务点
  49. endpoint ="http://192.168.20.32/projectname/control/SOAPService/";
  50. Call call = (Call)new Service().createCall();
  51. call.setTargetEndpointAddress(new URL(endpoint));
  52. //指明要调用的服务名称
  53. call.setOperationName(new QName("findSeniorService","findSeniorService"));
  54. //指明服务的输出输出参数
  55. call.addParameter("userid",
  56. org.apache.axis.Constants.XSD_STRING,
  57. javax.xml.rpc.ParameterMode.INOUT);
  58. call.addParameter("salt",
  59. org.apache.axis.Constants.XSD_STRING,
  60. javax.xml.rpc.ParameterMode.IN);
  61. call.addParameter("aaa",
  62. org.apache.axis.Constants.XSD_STRING,
  63. javax.xml.rpc.ParameterMode.OUT);
  64. call.addParameter("bbb",
  65. org.apache.axis.Constants.XSD_STRING,
  66. javax.xml.rpc.ParameterMode.OUT);
  67. call.addParameter("ccc",
  68. org.apache.axis.Constants.XSD_STRING,
  69. javax.xml.rpc.ParameterMode.OUT);
  70. call.setReturnType(org.apache.axis.Constants.XSD_STRING);
  71. //传递参数,发起调用
  72. Object responseWS = call.invoke(new Object[]{"123456789","aaa"});
  73. System.out.println("Receiving response: " + (String) responseWS);
  74. output = call.getOutputParams();
  75. getResponseParams(call.getMessageContext().getResponseMessage());
  76. }catch (MalformedURLException ex) {
  77. message ="error: wrong url";
  78. }catch (ServiceException ex) {
  79. message ="error: failed to create the call";
  80. }catch (RemoteException ex) {
  81. ex.printStackTrace();
  82. message ="error: failed to invoke WS";
  83. }finally {
  84. System.out.println("");
  85. System.out.println(message);
  86. }
  87. }
  88. }

注意上边的endpoint的链接要根据你服务器部署的实际情况来书写。

同时我也提供下xmlspy根据链接生成的数据包,这个不可用:

[c-sharp] view plaincopy
  1. <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">
  2. <SOAP-ENV:Body>
  3. <salt xsi:type="xsd:string">String</salt>
  4. <userid xsi:type="xsd:string">String</userid>
  5. </SOAP-ENV:Body>
  6. </SOAP-ENV:Envelope>

看到上边那个生成的不可用的文件主要是没指定服务方法,我们手工改一下,并将我们的参数值奉上:

[xhtml] view plaincopy
  1. <SOAP-ENV:Envelopexmlns: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">
  2. <SOAP-ENV:Body>
  3. <findSeniorServicexmlns="http://ofbiz.apache.org/service/">
  4. <saltxsi:type="xsd:string">aaa</salt>
  5. <useridxsi:type="xsd:string">2222</userid>
  6. </findSeniorService>
  7. </SOAP-ENV:Body>
  8. </SOAP-ENV:Envelope>

看上边只是指定了我们要给哪个方法传送参数“<findSeniorService xmlns="http://ofbiz.apache.org/service/">”

然后发送soap的信息到webservice接口,我这里的返回值如下:

[xhtml] view plaincopy
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <soapenv:Envelopexmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  3. <soapenv:Body>
  4. <ns1:findSeniorServiceResponsexmlns:ns1="http://ofbiz.apache.org/service/">
  5. <aaaxsi:type="xsd:string">test_aaaaa</aaa>
  6. <bbbxsi:type="xsd:string">test_bbbbb</bbb>
  7. <cccxsi:type="xsd:string">test_ccccc</ccc>
  8. <useridxsi:type="xsd:string">2222</userid>
  9. </ns1:findSeniorServiceResponse>
  10. </soapenv:Body>
  11. </soapenv:Envelope>

这样我就验证了虽然ofbiz提供的webservice的wsdl很不好用,但是那个webservice接口还是可以使用的。只不过只是支持基础数据类型而已。