當(dāng)柳上原的風(fēng)吹向天際的時候...

          真正的快樂來源于創(chuàng)造

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks
          WebService的原始API直接書寫在代碼中有諸多不便,如果我們把其調(diào)用過程歸納成一個類,再用Spring把URI和方法名注入到實(shí)例中去,這樣就好些了。

          歸納出來的WebService調(diào)用類:
          package com.heyang;

          import java.net.MalformedURLException;
          import java.rmi.RemoteException;

          import javax.xml.rpc.ServiceException;

          import org.apache.axis.client.Call;
          import org.apache.axis.client.Service;

          /**
           * WebService統(tǒng)一調(diào)用類
           * 
          @author: 何楊(heyang78@gmail.com)
           * @date: 2009-10-10-下午11:47:56
           
          */
          public class WebService{
              
          private String endpoint;
              
              
          private String operationName;
              
              
          /**
               * 取得WebService調(diào)用的結(jié)果
               * 
          @param args
               * 
          @return
               * 
          @throws ServiceException
               * 
          @throws MalformedURLException
               * 
          @throws RemoteException
               
          */
              
          public Object getCallResult(Object[] args)throws ServiceException, MalformedURLException, RemoteException{
                  
          // 創(chuàng)建 Service實(shí)例
                  Service service = new Service();
                  
                  
          // 通過Service實(shí)例創(chuàng)建Call的實(shí)例
                  Call call = (Call) service.createCall();
                  
                  
          // 將Web Service的服務(wù)路徑加入到call實(shí)例之中.
                  call.setTargetEndpointAddress(new java.net.URL(endpoint));// 為Call設(shè)置服務(wù)的位置
                  
                  
          // 調(diào)用Web Service的方法
                  call.setOperationName(operationName);
                  
                  
          // 調(diào)用Web Service,傳入?yún)?shù)
                  return call.invoke(args);
              }

              
          public String getEndpoint() {
                  
          return endpoint;
              }

              
          public void setEndpoint(String endpoint) {
                  
          this.endpoint = endpoint;
              }

              
          public String getOperationName() {
                  
          return operationName;
              }

              
          public void setOperationName(String operationName) {
                  
          this.operationName = operationName;
              }
          }

          再在上下文中配置三個bean,這樣WebService的相關(guān)信息就變成可配置方式了:
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

          <beans>
              
          <bean id="method_isExist" class="com.heyang.WebService" >    
                  
          <property name="endpoint">
                      
          <value>http://localhost:8080/UserLoginService/services/login</value>
                  
          </property>
                  
          <property name="operationName">
                      
          <value>isExist</value>
                  
          </property>
              
          </bean>
              
              
          <bean id="method_getUserRole" class="com.heyang.WebService" >    
                  
          <property name="endpoint">
                      
          <value>http://localhost:8080/UserLoginService/services/login</value>
                  
          </property>
                  
          <property name="operationName">
                      
          <value>getUserRole</value>
                  
          </property>
              
          </bean>
              
              
          <bean id="method_getUserTrade" class="com.heyang.WebService" >    
                  
          <property name="endpoint">
                      
          <value>http://localhost:8080/UserLoginService/services/login</value>
                  
          </property>
                  
          <property name="operationName">
                      
          <value>getUserTrade</value>
                  
          </property>
              
          </bean>
           
          </beans>

          調(diào)用因此也變得相對簡單:
          package com.heyang;

          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;

          /**
           * 使用Spring的簡約式調(diào)用WebService
           * 
          @author: 何楊(heyang78@gmail.com)
           * @date: 2009-10-10-下午11:40:49
           
          */
          public class SpringTest{
              
          public static void main(String[] args){
                  
          try{
                      ApplicationContext appContext 
          = new ClassPathXmlApplicationContext("bean.xml");
                      Object[] params
          =new Object[] { "MT001","123" };
                      
                      WebService ws1
          =(WebService)appContext.getBean("method_isExist");
                      WebService ws2
          =(WebService)appContext.getBean("method_getUserRole");
                      WebService ws3
          =(WebService)appContext.getBean("method_getUserTrade");
                      
                      System.out.println(ws1.getCallResult(params));
                      System.out.println(ws2.getCallResult(params));
                      System.out.println(ws3.getCallResult(params));
                  }
                  
          catch(Exception ex){
                      ex.printStackTrace();
                  }
              }
          }

          代碼下載(jar請從前面的程序里找,WebService即用上一篇文章中的UserLoginService):
          http://www.aygfsteel.com/Files/heyang/UserLoginServiceTest20091011082831.rar
          posted on 2009-10-11 00:10 何楊 閱讀(4268) 評論(1)  編輯  收藏

          Feedback

          # re: 使用Spring簡化對WebService的調(diào)用過程[未登錄] 2016-05-25 14:50 aa
          1111  回復(fù)  更多評論
            


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 秦安县| 庆元县| 繁昌县| 林周县| 诏安县| 习水县| 千阳县| 松阳县| 岑巩县| 吉木乃县| 平阴县| 昭平县| 灌阳县| 那坡县| 喀喇| 印江| 芷江| 蒲江县| 临邑县| 宿州市| 长治县| 黎平县| 织金县| 海城市| 牟定县| 普宁市| 阳城县| 庄浪县| 龙泉市| 紫金县| 宁陕县| 保德县| 化州市| 遵义市| 台中县| 深泽县| 凉城县| 滦平县| 玉龙| 彰化县| 阿拉尔市|