lqxue

          常用鏈接

          統計

          book

          tools

          最新評論

          java的call基于document/literal的webservice

          WSDL

          <definitions
               
          name="HelloWorld"
               targetNamespace
          ="http://xmlns.oracle.com/HelloWorld"
               xmlns
          ="http://schemas.xmlsoap.org/wsdl/"
               xmlns:plnk
          ="http://schemas.xmlsoap.org/ws/2003/05/partner-link/"
               xmlns:soap
          ="http://schemas.xmlsoap.org/wsdl/soap/"
               xmlns:client
          ="http://xmlns.oracle.com/HelloWorld"
              
          >
              
          <types>
                  
          <schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://xmlns.oracle.com/HelloWorld"
                       xmlns
          ="http://www.w3.org/2001/XMLSchema">
                      
          <element name="HelloWorldProcessRequest">
                          
          <complexType>
                              
          <sequence>
                                  
          <element name="input" type="string"/>
                              
          </sequence>
                          
          </complexType>
                      
          </element>
                      
          <element name="HelloWorldProcessResponse">
                          
          <complexType>
                              
          <sequence>
                                  
          <element name="result" type="string"/>
                              
          </sequence>
                          
          </complexType>
                      
          </element>
                  
          </schema>
              
          </types>
              
          <message name="HelloWorldRequestMessage">
                  
          <part name="payload" element="client:HelloWorldProcessRequest"/>
              
          </message>
              
          <message name="HelloWorldResponseMessage">
                  
          <part name="payload" element="client:HelloWorldProcessResponse"/>
              
          </message>
              
          <portType name="HelloWorld">
                  
          <operation name="process">
                      
          <input message="client:HelloWorldRequestMessage"/>
                      
          <output message="client:HelloWorldResponseMessage"/>
                  
          </operation>
              
          </portType>
              
          <binding name="HelloWorldBinding" type="client:HelloWorld">
                  
          <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
                  
          <operation name="process">
                      
          <soap:operation style="document" soapAction="process"/>
                      
          <input>
                          
          <soap:body use="literal"/>
                      
          </input>
                      
          <output>
                          
          <soap:body use="literal"/>
                      
          </output>
                  
          </operation>
              
          </binding>
              
          <service name="HelloWorld">
                  
          <port name="HelloWorldPort" binding="client:HelloWorldBinding">
                      
          <soap:address location="http://robin:9700/orabpel/default/HelloWorld/1.0"/>
                  
          </port>
              
          </service>
            
          <plnk:partnerLinkType name="HelloWorld">
              
          <plnk:role name="HelloWorldProvider">
                
          <plnk:portType name="client:HelloWorld"/>
              
          </plnk:role>
            
          </plnk:partnerLinkType>

          </definitions>


          Java 代碼:
          import java.net.MalformedURLException;
          import java.net.URL;
          import java.rmi.RemoteException;
          import java.util.Vector;

          import javax.xml.namespace.QName;
          import javax.xml.parsers.DocumentBuilder;
          import javax.xml.parsers.DocumentBuilderFactory;
          import javax.xml.parsers.FactoryConfigurationError;
          import javax.xml.parsers.ParserConfigurationException;
          import javax.xml.rpc.ServiceException;

          import org.apache.axis.client.Call;
          import org.apache.axis.constants.Style;
          import org.apache.axis.message.SOAPBodyElement;
          import org.apache.xml.serialize.DOMSerializerImpl;
          import org.apache.xml.serialize.OutputFormat;
          import org.w3c.dom.Document;
          import org.w3c.dom.Element;

          public class BPELServiceTest {
                
          //service的命名空間
              static final String ns = "http://xmlns.oracle.com/HelloWorld";

              
          public static void main(String args[]){
                  Call call 
          = null;
                  
          try {
                      call 
          = createCall();
                      Vector rtn 
          = (Vector) call.invoke(createRequest());
                      parse(rtn);
                  }
           catch (MalformedURLException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
           catch (RemoteException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
           catch (ParserConfigurationException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
           catch (FactoryConfigurationError e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
           catch (Exception e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }


              }


                
          /*       * 創建Call對象,對設置相關屬性,注意:其中的屬性應該是通過分析WSDL文件由程序動態獲得來賦值,       * 這里全部簡化為靜態賦值       */
              
          static Call createCall() throws MalformedURLException, ServiceException{
                  org.apache.axis.client.Service s 
          = new org.apache.axis.client.Service();
                  Call call 
          = (Call) s.createCall();
                  call.setTargetEndpointAddress(
          new URL("http://robin:9700/orabpel/default/HelloWorld/1.0"));
                  call.setSOAPActionURI(
          "process");
                  call.setOperationName(
          "process");
                  call.setProperty(Call.OPERATION_STYLE_PROPERTY, Style.DOCUMENT.getName());
                  call.setPortName(
          new QName(ns, "HelloWorldPort"));
                  call.setPortTypeName(
          new QName(ns, "HelloWorld"));

                  
          return call;
              }


                
          /*       *創建請求參數,實際上就是構建DOM片斷,根據Web service對輸入參數的要求來構建,要多復雜,都可以實現,       *這就是Docuemnt的好處,省去了復雜對象的序列化。       */
              
          static Object[] createRequest() throws ParserConfigurationException, FactoryConfigurationError{
                  DocumentBuilder db 
          = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                  Document doc 
          = db.newDocument();
                  Element root 
          = doc.createElementNS(ns, "HelloWorldProcessRequest");
                  Element input 
          = doc.createElementNS(ns, "input");
                  input.appendChild(doc.createTextNode(
          "robin"));
                  root.appendChild(input);
                  doc.appendChild(root);

                  
          return new Object[]{new SOAPBodyElement(root)};
              }


                
          // 對返回結果進行解析,并打印。
              static void parse(Vector v) throws Exception{
                  Document doc 
          = ((SOAPBodyElement) v.get(0)).getAsDocument();
                  Element root 
          = doc.getDocumentElement();
                  OutputFormat of 
          = new OutputFormat();
                  of.setIndent(
          4);
                  System.out.println(
          new DOMSerializerImpl().writeToString(root));
              }

          }



          上述代碼運行輸出結果為:

          <?xml version="1.0"?>
          <HelloWorldProcessResponse xmlns="http://xmlns.oracle.com/HelloWorld">
          <result xmlns="http://xmlns.oracle.com/HelloWorld">robin</result>
          </HelloWorldProcessResponse>



          上面的代碼很簡單,需要說明的是:采用Document調用,實際上invoke方法的參數是一個元素類型為SOAPBodyElement的對象數組,而返回結果是一個元素類型的SOAPBodyElement的Vector對象。

           

          From:http://www.javaeye.com/topic/138876

          posted on 2008-05-26 11:09 lqx 閱讀(1543) 評論(1)  編輯  收藏 所屬分類: web service

          評論

          # re: java的call基于document/literal的webservice 2008-06-02 15:01 liguoxin

          很不錯的,不知道用java調.net寫的web service是不是也只這樣寫的。.net的web service不就是document的嗎  回復  更多評論   

          主站蜘蛛池模板: 阿坝县| 永仁县| 宜都市| 惠水县| 临泽县| 堆龙德庆县| 滕州市| 龙里县| 清水县| 高陵县| 昌江| 渭源县| 英吉沙县| 壤塘县| 昭觉县| 平潭县| 凤城市| 峨边| 临沂市| 平罗县| 嵊州市| 泾源县| 绥化市| 永靖县| 二连浩特市| 桐乡市| 南江县| 正定县| 随州市| 长顺县| 安康市| 江华| 桦南县| 徐州市| 虞城县| 瑞金市| 郧西县| 宁安市| 廊坊市| 尉犁县| 麻江县|