七.编写客户端进行测试
我们使用application的方式编写一个客户端程序对刚才发布的EJB和Web Service进行测试。代码如下:
package com.webservice.client;
import java.net.*;
import javax.xml.namespace.*;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.*;
import org.apache.axis.encoding.*;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import com.pojo.*;
public class PersonManagerClinet
{
public static void main( String args[] ) throws Exception
{
String endpoint = "http://localhost:8080/jboss-net/services/PersonManagerService";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(endpoint));
QName qn = new QName("http://localhost:8080/pojo/person", "Person");
call.registerTypeMapping(Person.class, qn, new BeanSerializerFactory(
Person.class, qn),
new BeanDeserializerFactory(Person.class, qn));
Person person = null;
call.setOperationName("storePerson");
call.addParameter("person", XMLType.XSD_ENTITY, ParameterMode.IN);
person = new Person("java");
call.invoke(new Object[] {person});
System.out.println("OK!");
call.setOperationName("getPersonByName");
call.addParameter("name", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(qn);
person = (Person) call.invoke(new Object[] {"java"});
System.out.println(person);
}
}
运行结果如下:

可以看到服务已经运行了,并返回了正确的结果。到此我们已经完成了在Jboss环境下Web Service调用EJB的开发,另外jboss自从4.0开始升级了其Web Service模块,由原来的jboss-net升级到了ws4ee,而且部署和发布也发生很大的变化,可以说,在3.2下的调试成功的Web Service在4.0上是无法运行的,目前在jboss的官方网站上有一篇关于在jboss4.0上配置web service的文章。有兴趣的人可以去查阅一下。
