axis2 webservice 小例子
参考网上写的。 ? 1 在Apache 的官网下载axis2 的包,这时候下载axis2.war.zip 的包, ?? 下载后解压,将axis2.war包放到tomcat 的webapps下面,启动tomcat ? 2 在地址栏中输入http://localhost:8899/axis2 ?? 出现熟悉的界面,说明Ok了,一般都是好的。 ? ? 3 好了我们自学着写个例子,这时候可以参考axis2中自带的一个version的例子。 ? ?? ?简单的还是从Hello例子写起。 ?? ?/** ?*? ?*/ package axis2; ? public class Hello { ? public String hello(){ return "Hello test"; ?} } ? 建立meta-inf目录,编写services.xml文件
<service name="Hello"> <Description> HEllO example description ?? ?</Description> <parameter name="ServiceClass" locked="false">axis2.Hello</parameter> <operation name="hello"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> </service> ? 将整个工程打成jar包,放到axis2下的WEB-INF下的services下,并改后缀为aar,重启tomcat。 http://localhost:8080/axis2/services/listServices,这是就可以看到服务了。(最好在IE中,像chrom就看不到wsdl) ? ? ? ========编写客户端测试程序===================
public class Exec { ? /** * @param args * @throws ServiceException * @throws MalformedURLException * @throws RemoteException */ public static void main(String[] args) throws ServiceException, MalformedURLException,RemoteException { // 1 method1 RPCServiceClient rpcClient = new RPCServiceClient(); Options opt = new Options(); opt.setTo(new EndpointReference( "http://localhost:8899/axis2/services/Hello")); // 服务地址 opt.setAction("urn:hello"); // 方法 rpcClient.setOptions(opt); OMElement element = rpcClient.invokeBlocking(new QName("http://axis2",? //查看wsdl时可以找到的 "hello"),new Object[] { null }); // null表示没有参数传递 ? Iterator values = element.getChildrenWithName(new QName("http://axis2", "return")); // return表示有返回值 System.out.println(values); while (values.hasNext()) { // 遍历出获取的数据 OMElement omElement = (OMElement) values.next(); System.out.println(omElement.getText()); } ? // 2 method2 String method = "hello"; Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(new java.net.URL( "http://localhost:8899/axis2/services/Hello")); call.setOperationName(new QName("http://com/",method)); call.setUseSOAPAction(true); call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING); call.setSOAPActionURI("http://com/GetServerList"); String k = (String) call.invoke(new Object[] {}); // // 因为返回值是String类型,所以这里调用的返回值也是String类型 System.out.println(">>> " + k); // 返回值输出 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |