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