微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

转 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.  
  13. public class AxisClient {     
  14.      /**
  15.       * 将我们的消息在控制台System.out打印出来
  16.       * */ 
  17.      private static Map getResponseParams(Message respMessage) { 
  18.             Map mRet = new Hashtable(); 
  19.             try
  20. SOAPEnvelope resEnv = respMessage.getSOAPEnvelope();
  21.                 List bodies = resEnv.getBodyElements(); 
  22.                 Iterator i = bodies.iterator(); 
  23.                 while (i.hasNext()) { 
  24.                     Object o = i.next(); 
  25.                     if (o instanceof RPCElement) { 
  26.                         RPCElement body = (RPCElement) o; 
  27.                         List params = null
  28.                         params = body.getParams(); 
  29.                         Iterator p = params.iterator(); 
  30.                         while (p.hasNext()) { 
  31.                             RPCParam param = (RPCParam) p.next(); 
  32.                             mRet.put(param.getName(),param.getValue()); 
  33.                             System.out.println("SOAP Client Param - " + param.getName() + "=" + param.getValue()); 
  34.                         } 
  35.                     } 
  36.                 } 
  37.             } catch (org.apache.axis.AxisFault e) { 
  38.              System.out.println("AxisFault"); 
  39.             } catch (org.xml.sax.SAXException e) { 
  40.              System.out.println("SAXException"); 
  41.             } 
  42.             return mRet; 
  43.         } 
  44.         
  45.         public static void main(String[] args) { 
  46.         String message = ""
  47.         Map output; 
  48.         String endpoint; 
  49.         try
  50.             //指明我们的服务点 
  51.             endpoint = "http://192.168.20.32/projectname/control/SOAPService/"
  52.             Call call = (Call) new Service().createCall(); 
  53.             call.setTargetEndpointAddress(new URL(endpoint)); 
  54.             //指明要调用的服务名称 
  55.             call.setoperationName(new QName("findSeniorService","findSeniorService")); 
  56.             //指明服务的输出输出参数 
  57.             call.addParameter("userid"
  58.                               org.apache.axis.Constants.XSD_STRING, 
  59.                               javax.xml.rpc.ParameterMode.INOUT); 
  60.             call.addParameter("salt"
  61.                     org.apache.axis.Constants.XSD_STRING, 
  62.                     javax.xml.rpc.ParameterMode.IN); 
  63.             call.addParameter("aaa",108); border-left-width:3px; list-style-type:decimal-leading-zero; list-style-position:outside!important">                     org.apache.axis.Constants.XSD_STRING,248)">                     javax.xml.rpc.ParameterMode.OUT); 
  64.             call.addParameter("bbb",108); border-left-width:3px; list-style-type:decimal-leading-zero; list-style-position:outside!important">                     javax.xml.rpc.ParameterMode.OUT); 
  65.             call.addParameter("ccc",108); border-left-width:3px; list-style-type:decimal-leading-zero; list-style-position:outside!important">             call.setReturnType(org.apache.axis.Constants.XSD_STRING); 
  66.             //传递参数,发起调用 
  67.             Object responseWS = call.invoke(new Object[]{"123456789","aaa"}); 
  68.             System.out.println( "Receiving response: " +  (String) responseWS); 
  69.             output = call.getoutputParams(); 
  70.             getResponseParams(call.getMessageContext().getResponseMessage()); 
  71.         } catch (MalformedURLException ex) { 
  72.             message = "error: wrong url"
  73.         } catch (ServiceException ex) { 
  74.             message = "error: Failed to create the call"
  75.         } catch (remoteexception ex) { 
  76.             ex.printstacktrace(); 
  77.             message = "error: Failed to invoke WS"
  78.         } finally
  79.           System.out.println(""); 
  80.           System.out.println(message); 
  81.       } 

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

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

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

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

[xhtml] copy
    <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"> 
  1.     <SOAP-ENV:Body> 
  2.       <findSeniorService xmlns="http://ofbiz.apache.org/service/"> 
  3.         <salt xsi:type="xsd:string">aaa</salt> 
  4.         <userid xsi:type="xsd:string">2222</userid> 
  5.       </findSeniorService> 
  6.     </SOAP-ENV:Body> 
  7. </SOAP-ENV:Envelope> 

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

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

copy
    <?xml version="1.0" encoding="UTF-8"?> 
  1. <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"> 
  2.     <soapenv:Body> 
  3.         <ns1:findSeniorServiceResponse xmlns:ns1="http://ofbiz.apache.org/service/"> 
  4.             <aaa xsi:type="xsd:string">test_aaaaa</aaa> 
  5.             <bbb xsi:type="xsd:string">test_bbbbb</bbb> 
  6.             <ccc xsi:type="xsd:string">test_ccccc</ccc> 
  7.             <userid xsi:type="xsd:string">2222</userid> 
  8.         </ns1:findSeniorServiceResponse> 
  9.     </soapenv:Body> 
  10. </soapenv:Envelope> 

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐