當柳上原的風吹向天際的時候...

          真正的快樂來源于創造

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

          歸納出來的WebService調用類:
          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統一調用類
           * 
          @author: 何楊(heyang78@gmail.com)
           * @date: 2009-10-10-下午11:47:56
           
          */
          public class WebService{
              
          private String endpoint;
              
              
          private String operationName;
              
              
          /**
               * 取得WebService調用的結果
               * 
          @param args
               * 
          @return
               * 
          @throws ServiceException
               * 
          @throws MalformedURLException
               * 
          @throws RemoteException
               
          */
              
          public Object getCallResult(Object[] args)throws ServiceException, MalformedURLException, RemoteException{
                  
          // 創建 Service實例
                  Service service = new Service();
                  
                  
          // 通過Service實例創建Call的實例
                  Call call = (Call) service.createCall();
                  
                  
          // 將Web Service的服務路徑加入到call實例之中.
                  call.setTargetEndpointAddress(new java.net.URL(endpoint));// 為Call設置服務的位置
                  
                  
          // 調用Web Service的方法
                  call.setOperationName(operationName);
                  
                  
          // 調用Web Service,傳入參數
                  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的相關信息就變成可配置方式了:
          <?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>

          調用因此也變得相對簡單:
          package com.heyang;

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

          /**
           * 使用Spring的簡約式調用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的調用過程[未登錄] 2016-05-25 14:50 aa
          1111  回復  更多評論
            


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


          網站導航:
           
          主站蜘蛛池模板: 桑日县| 漳平市| 横峰县| 阳泉市| 合肥市| 开鲁县| 安吉县| 英超| 东源县| 清新县| 五峰| 夏津县| 博客| 沭阳县| 东方市| 崇礼县| 蒙阴县| 乐东| 娱乐| 阿拉尔市| 芦山县| 马龙县| 蒙城县| 金乡县| 玉门市| 洛南县| 临武县| 黔江区| 时尚| 翼城县| 阿勒泰市| 邓州市| 平南县| 定南县| 凉城县| 赣榆县| 满洲里市| 潮安县| 青阳县| 自贡市| 时尚|