JDK版本:1.7update65
Eclipse版本:Juno Service Release 2(4.2.2)
插件安装过程及WSDL文件生成过程参见:http://blog.csdn.net/a19881029/article/details/40194787
1,下载Axis2的1.6.2版本二进制压缩包,下载路径为:http://axis.apache.org/axis2/java/core/download.cgi,下载完成后解压,以供之后使用
2,创建一个普通的Java工程,用来存放自动生成的Web Service服务端/客户端代码
3,选择Eclipse菜单栏中的File -> New -> Other...,在弹出的对话框中选择Axis2 Code Generator,然后选择Next
4,选择通过WSDL文件生成Java源代码
5,选择生成好的WSDL文件
6,如果Codegen option选择default,则不可修改下面的配置项,默认生成Web Service客户端,如果Codegen option选择custom,则可以手动修改下面的配置项
7,选择将自动生成的Java代码保存在Eclipse当前工作空间中的特定工程中
8,虽然Axis2依赖的jar包加到了工程中,但是并没有添加到工程的Build Path中,需要手动添加一下
9,客户端生成代码如下:
生成的时候选择同时生成同步方法和异步方法,同步方法比较简单,就不多说了,这里看下MathStub类中异步方法是如何处理的
...... public void startadd( com.sean.Add add2, final com.sean.MathCallbackHandler callback) throws java.rmi.RemoteException{ ...... callback.receiveResultadd( (com.sean.AddResponse)object); } catch (org.apache.axis2.AxisFault e) { cllback.receiveErroradd(e); } ......
MathCallbackHandler是个抽象方法
...... /** * auto generated Axis2 call back method for add method override this method * for handling normal response from add operation */ public void receiveResultadd(com.sean.AddResponse result) {} /** * auto generated Axis2 Error handler override this method for handling * error response from add operation */ public void receiveErroradd(java.lang.Exception e) {} ......
综上,实现了自己的异常处理类
package com.sean; public class MyMathCallbackHandler extends MathCallbackHandler { @Override public void receiveResultadd(AddResponse result) { super.clientData = result; } @Override public void receiveErroradd(Exception e) { System.out.println(e.getMessage()); } }
接口调用方式如下
package com.sean; public class Test { public static void main(String[] args) throws Exception { Math math = new MathStub("http://127.0.0.1:8888/mockMathSoap12Binding"); //sync Add add1 = new Add(); add1.setArgs0(1); add1.setArgs1(2); AddResponse resp1 = math.add(add1); System.out.println(resp1.get_return()); //async Add add2 = new Add(); add2.setArgs0(5); add2.setArgs1(6); MathCallbackHandler callback = new MyMathCallbackHandler(); math.startadd(add2, callback); //As the method is not sync, can't get result immediately. Thread.sleep(1000); AddResponse resp2 = (AddResponse)callback.getClientData(); System.out.println(resp2.get_return()); } }
10,服务端生成代码如下:
是否选择生成Sleleton的接口类都可以,如果勾选,则会多生成一个MathSkeletonInterface类,MathSkeleton类变为该接口类的实现类,在MathMessageReceiverInOut中,由直接调用MathSkeleton类改为调用MathSkeletonInterface接口类,除此之外没有别的变化
MathSkeleton类中需要添加服务端的处理逻辑
/** * MathSkeleton.java * * This file was auto-generated from WSDL * by the Apache Axis2 version: 1.6.2 Built on : Apr 17, 2012 (05:33:49 IST) */ package com.sean; public class MathSkeleton implements MathSkeletonInterface { public com.sean.AddResponse add(com.sean.Add add0) { int a = add0.getArgs0(); int b = add0.getArgs1(); int result = a + b; AddResponse resp = new AddResponse(); resp.set_return(result); return resp; } }