当柳上原的风吹向天际的时候...

真正的快乐来源于创造

  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.blogjava.net/Files/heyang/UserLoginServiceTest20091011082831.rar
posted on 2009-10-11 00:10 何杨 阅读(4257) 评论(1)  编辑  收藏

Feedback

# re: 使用Spring简化对WebService的调用过程[未登录] 2016-05-25 14:50 aa
1111  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: