<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    真正的快樂來源于創造

      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://m.tkk7.com/Files/heyang/UserLoginServiceTest20091011082831.rar
    posted on 2009-10-11 00:10 何楊 閱讀(4265) 評論(1)  編輯  收藏

    Feedback

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


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


    網站導航:
     
    主站蜘蛛池模板: 久久亚洲国产欧洲精品一| 久久精品国产精品亚洲人人| 777亚洲精品乱码久久久久久| 国产精品永久免费视频| 亚洲美女在线国产| 亚欧洲精品在线视频免费观看 | 思思re热免费精品视频66| 亚洲高清中文字幕| 可以免费看黄视频的网站| 中文文字幕文字幕亚洲色| 女人18毛片特级一级免费视频| 国产精品高清视亚洲一区二区| 超pen个人视频国产免费观看| 美女18毛片免费视频| 亚洲精品国产自在久久 | 国产亚洲精品美女2020久久| 免费在线观看毛片| 国产午夜无码精品免费看| 亚洲av永久无码精品网站 | 亚洲熟妇无码乱子AV电影| 日韩吃奶摸下AA片免费观看| 3344免费播放观看视频| 青青操免费在线观看| 美女被免费网站在线视频免费| 久久久久久亚洲精品不卡| 中文字幕在线免费播放| 亚洲国产成人久久精品影视 | 一本一道dvd在线观看免费视频 | 韩国日本好看电影免费看| 成人a毛片免费视频观看| 亚洲精品无码MV在线观看| 真人做人试看60分钟免费视频| 亚洲最大天堂无码精品区| 亚洲国产主播精品极品网红| 18禁超污无遮挡无码免费网站| 亚洲无圣光一区二区| 国产在线19禁免费观看国产 | 在线看免费观看AV深夜影院| 国产成人亚洲综合一区 | 亚洲精品私拍国产福利在线| 成人毛片免费观看视频大全|