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

 

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

  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. public class AxisClient {
  13. /**
  14. * 将我们的消息在控制台System.out打印出来
  15. * */
  16. private static 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. public static void 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 plain copy
  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 plain copy
  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. <findSeniorService xmlns="http://ofbiz.apache.org/service/">
  4. <salt xsi:type="xsd:string">aaa</salt>
  5. <userid xsi: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 plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <soapenv:Body>
  4. <ns1:findSeniorServiceResponse xmlns:ns1="http://ofbiz.apache.org/service/">
  5. <aaa xsi:type="xsd:string">test_aaaaa</aaa>
  6. <bbb xsi:type="xsd:string">test_bbbbb</bbb>
  7. <ccc xsi:type="xsd:string">test_ccccc</ccc>
  8. <userid xsi:type="xsd:string">2222</userid>
  9. </ns1:findSeniorServiceResponse>
  10. </soapenv:Body>
  11. </soapenv:Envelope>

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值