《WebService的开发模版(Axis2_15环境).docx》由会员分享,可在线阅读,更多相关《WebService的开发模版(Axis2_15环境).docx(6页珍藏版)》请在三一办公上搜索。
1、WebService的开发模版(Axis2 1.5环境)Axis2已经升级到1.5了,下载地址:两个包,bin是开发包,war是部署包。环境:axis2-1.5jdk1.5一、创建项目导创建一个Java项目,项目结构如下图:Servicesrc pojo META-INF二、导入axis2的包解压缩axis2-1.5-bin.zip到一个目录下,然后直接引入axis2的jar包即可。也可以讲axis2的包复制到lib下。三、创建服务axis2服务的创建过程就是写Java类。这里创建一个最简单的Service,作为发布对象。package pojo; /* * 测试Service * * aut
2、hor leizhimin 2009-8-4 15:48:13 */ public class SimpleService public String doSomething(String taskname) System.out.println(MyServiceImpl is calling doSomething with + taskname + !); return taskname + is finished!; 把创建好的SimpleService.class文件拷贝到axis的pojo下面 ,如果没有pojo文件夹则自己创建一个。四、描述服务在项目的srcMETA-INF文件夹
3、下面,创建services.xml,内容如下:service name= SimpleService scope=application targetNamespace= pojo.SimpleService 服务名:SimpleService服务实现类:pojo.SimpleService生成SimpleService.aar文件,目的是类文件可以带包名进行发布服务 1.cmd-进入到项目的“src”目录执行下面命令-“jar -cvf SimpleService.aar META-INF”;五、部署服务将下载的axis2-1.5-war.zip解开,得到一个axis2.war,将其解压缩到
4、tomcat的默认的web发布目录webapp下面,比如,在我机器上会产生F:apache-tomcat-6.0.20webappsaxis2的目录,目录下面就是axis2.war里面的内容。然后,将生成的SimpleService.aar复制到F:apache-tomcat-6.0.20webappsaxis2WEB-INFservices下面。八、启动tomcat,查看发布服务浏览axis2的服务点Services,查看所有的服务在点击服务连接,就可以查看wsdl了:经过这样的测试后,服务发布算成功了。六、客户端服务package pojo;import java.io.ByteArray
5、InputStream;import java.io.IOException;import java.io.InputStream;import .MalformedURLException;import .URL;import .URLConnection;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.Map;import java.util.Timer;import java.util.TimerTask;import javax.xml.namespac
6、e.QName;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.rpc.ServiceException;import org.apache.axis.client.Call;import org.apache.axis.client.Service;import org.apache.axis2.addressing.EndpointReference;import org.apache.axis2.client.Options;
7、import org.apache.axis2.rpc.client.RPCServiceClient;public class RPCClient public static void main(String args) throws Exception try / 使用RPC方式调用WebService RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); / 指定调用WebService的URL EndpointReference tar
8、getEPR = new EndpointReference( http:/localhost:8007/axis2/services/SimpleService); options.setTo(targetEPR); / 指定getGreeting方法的参数值 Object opAddEntryArgs = new Object 2011-1-25测试; / 指定getGreeting方法返回值的数据类型的Class对象 Class classes = new Class String.class; / 指定要调用的getGreeting方法及WSDL文件的命名空间 QName opAddEntry = new QName(http:/pojo, QueryCompany); / 调用getGreeting方法并输出该方法的返回值 String xml =(String)serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)0; System.out.println(xml); catch (Exception e) e.printStackTrace();