上接XFire+Spring发布Web Service(二)
3.客户端编程测试
下面我们再看看如何对其进行客户端编程。
客户端调用方式有两种,一种为使用服务端的窄接口类,XFire根据Service模型对象及Web Service的URL地址就可以构造出Web Service的调用实例。在服务端Tomcat启动的情况下,运行以下的客户 代码,将可以获得正确的输出。
package demo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/**
* @author dreava
* Nov 12, 2008
*/
public class ServiceTest {
public void invokeService() throws Exception{
//根据窄接口创建Service模型
Service serviceModel = new ObjectServiceFactory()
.create(IHelloService.class);
//服务对应的URL地址
String serviceURL = "http://localhost:8080/springxfire/service/HelloServiceUT";
IHelloService service = null;
try {
//创建一个Web Service的访问实例,并将其转换为窄接口类型。
service = (IHelloService) new XFireProxyFactory().create(
serviceModel, serviceURL);
} catch (Exception e) {
throw new RuntimeException(e);
}
//以下为调用各个Web Service方法
//Test sayHello
String str = service.sayHello("dreava");
System.out.println("sayHello(): " + str);
//Test getList
List<String> list = new ArrayList<String>();
for(int i = 0; i < 5; i ++){
list.add("this is: " + i);
}
List<Course> courseList = (List<Course>)service.getList(list);
System.out.println("getList(): ");
for (Course course : courseList) {
System.out.println(course.getName());
}
//Test choose
User user = new User();
user.setName("dreava");
Course course = service.choose(user);
System.out.println("choose(): ");
System.out.println(course.getName());
//Test getCollection
Collection<User> collection = service.getCollection();
System.out.println("getCollection(): ");
for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
User user2 = (User) iterator.next();
System.out.println(user2.getName());
}
}
public static void main(String[] args) throws Exception{
ServiceTest client = new ServiceTest();
client.invokeService();
}
}
另一种调用方式是,用户可以通过WSDL文件生成客户端调用程序,用户可以直接通过URL指定WSDL,也可以将WSDL保存在本地系统中,通过InputStream的方式获取WSDL内容。代码如下:
String wsdl = "test/HelloServiceUT.wsdl";
Resource resource = new ClassPathResource(wsdl);
Client client = new Client(resource.getInputStream(), null);
//Test sayHello
Object[] helloResults = client.invoke("sayHello", new Object[]{"dreava"});
System.out.println("Test sayHello: " + helloResults[0]);
//Test choose
User user = new User();
user.setName("liaokun");
Object[] chooseResults = client.invoke("choose", new Object[]{user});
if(chooseResults != null){
//以下代码将抛出类型转换异常
Course course = (Course) chooseResul
System.out.println("Test choose, the result course is " + course.getName());
}
//Test getList
List<String> list = new ArrayList<String>();
for(int i = 0; i < 5; i ++){
list.add("this is: " + i);
}
//以下代码将抛出异常,该问题的解决还有待进一步上网查找解决办法。
//Object[] testResults = client.invoke("getList", new Object[]{list});
对于涉及到复杂对象的Web Service,必须要配置一个映射文件,比如IHelloService.aegis.xml,该文件必须放在与发布Web Service的接口文件相同的目录下,映射配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<mappings>
<mapping>
<method name="choose">
<parameter index="0" componentType="java.lang.String" />
<return-type componentType="demo.Course" />
</method>
<method name="getList">
<parameter index="0" componentType="java.lang.String" />
<return-type componentType="demo.Course" />
</method>
<method name="getCollection">
<parameter index="0" componentType="java.lang.String" />
<return-type componentType="demo.User" />
</method>
</mapping>
</mappings>
通过上面的例子,在实际项目当中只需要稍加扩展即可,这个例子应该能够应付Web Service项目的开发或改造了。