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

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

 

[java]  view plain copy
  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.   
  49.         String endpoint;  
  50.         try {  
  51.             //指明我们的服务点  
  52.             endpoint = "http://192.168.20.32/projectname/control/SOAPService/";  
  53.             Call call = (Call) new Service().createCall();  
  54.             call.setTargetEndpointAddress(new URL(endpoint));  
  55.             //指明要调用的服务名称  
  56.             call.setOperationName(new QName("findSeniorService""findSeniorService"));  
  57.             //指明服务的输出输出参数  
  58.             call.addParameter("userid",  
  59.                               org.apache.axis.Constants.XSD_STRING,  
  60.                               javax.xml.rpc.ParameterMode.INOUT);  
  61.             call.addParameter("salt",  
  62.                     org.apache.axis.Constants.XSD_STRING,  
  63.                     javax.xml.rpc.ParameterMode.IN);  
  64.             call.addParameter("aaa",  
  65.                     org.apache.axis.Constants.XSD_STRING,  
  66.                     javax.xml.rpc.ParameterMode.OUT);  
  67.             call.addParameter("bbb",  
  68.                     org.apache.axis.Constants.XSD_STRING,  
  69.                     javax.xml.rpc.ParameterMode.OUT);  
  70.             call.addParameter("ccc",  
  71.                     org.apache.axis.Constants.XSD_STRING,  
  72.                     javax.xml.rpc.ParameterMode.OUT);  
  73.             call.setReturnType(org.apache.axis.Constants.XSD_STRING);  
  74.             //传递参数,发起调用  
  75.             Object responseWS = call.invoke(new Object[]{"123456789""aaa"});  
  76.             System.out.println( "Receiving response: " +  (String) responseWS);  
  77.             output = call.getOutputParams();  
  78.             getResponseParams(call.getMessageContext().getResponseMessage());  
  79.         } catch (MalformedURLException ex) {  
  80.             message = "error: wrong url";  
  81.         } catch (ServiceException ex) {  
  82.             message = "error: failed to create the call";  
  83.         } catch (RemoteException ex) {  
  84.             ex.printStackTrace();  
  85.             message = "error: failed to invoke WS";  
  86.         } finally {  
  87.           System.out.println("");  
  88.           System.out.println(message);  
  89.         }  
  90.       }  
  91. }  

 

 注意上边的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接口还是可以使用的。只不过只是支持基础数据类型而已。

你可能感兴趣的:(ofbiz的webservice接口提供(3)-不规范的wsdl的客户端访问代码)