【webservice】axis1生成java客户端代码
转载请说明出处:http://blog.csdn.net/seedingly/article/details/24806447
版本:axis-bin-1_4.zip (axis1)
事情始末:接触了webservice也有半年多了,期间都是用axis2(1.5.4),成功调通过各种wsdl,当然包括CXF的webservice。这时候,对一个axis弄出来的wsdl执行wsdl2java居然报错了,报错如下:
Exception in thread "main" org.apache.axis2.wsdl.codegen.CodeGenerationException : java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at org.apache.axis2.wsdl.codegen.CodeGenerationEngine.generate(CodeGener ationEngine.java:271) at org.apache.axis2.wsdl.WSDL2Code.main(WSDL2Code.java:35) at org.apache.axis2.wsdl.WSDL2Java.main(WSDL2Java.java:24) Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetExcepti on at org.apache.axis2.wsdl.codegen.extension.SimpleDBExtension.engage(Simp leDBExtension.java:53) at org.apache.axis2.wsdl.codegen.CodeGenerationEngine.generate(CodeGener ationEngine.java:224) ... 2 more
我查了很多资料,得出结论是wsdl是有错误的。最后,webservice提供方纠正了我的说法:这是axis1,要用同版本执行wsdl2java!得到了服务端的技术建议,当然是立马尝试啦,过程如下:
1、下载axis1的包,我下载的是axis-bin-1_4.zip ,同时也下载了activation-1.1.1.jar、mail-1.4.5.jar,因为网上的教程也建议添加上这两个包(不然会报错的)。最后包如下:
2、解压axis到F:\Learning\WebService\AXIS\目录,自行编写脚本(格式为bat,假设命名为wsdl2j.bat),双击执行生成java client代码。脚本如下:
set Axis_Lib=F:\Learning\WebService\AXIS\axis-1_4\lib set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib% set Output_Path=F:\src set Package=com.prc.client %Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% xxxx.wsdl
参数解析:
Axis_Lib——Axis jar包存放的文件夹
Output_Path——生成java类存放在本地的目录
Package——生成java类所存放的包
脚本末尾的wsdl路径也可以为url地址,如http://xxxx/xxx?wsdl
3、把生成的java类复制到我的webservice工程,生成的类比较多,包括总的类4个(下面用到IBaseServiceService、IBaseServiceServiceLocator、IBaseServiceSoapBindingStub)、每个webservice方法对应的两个类(方法类+返回result类)。
4、编程示例:
public String mmsc_SysBlackListRead(String streamingNo,int srcDeviceType,String srcDeviceID,String cmd){ String jsondata = ""; try { URL url = new URL("http://*******"); IBaseServiceService service = new IBaseServiceServiceLocator(); IBaseServiceSoapBindingStub stub = (IBaseServiceSoapBindingStub)service.getIBaseService(url); SysBlackListReadResult rs = stub.sysBlackListRead(streamingNo, srcDeviceType, srcDeviceID, cmd); HashMap dataMap = new HashMap(); dataMap.put("streamingNo", rs.getStreamingNo()); dataMap.put("resultCode", rs.getResultCode()); dataMap.put("SysBlackList", rs.getSysBlackList()); JSONObject jsons = JSONObject.fromObject(dataMap); jsondata = jsons.toString(); } catch ( Exception e2) { e2.printStackTrace(); } return jsondata; }其中,这个rs(SysBlackListReadResult )则是返回的结果,接下来的代码只不过是把rs弄成json格式而已,实际代码就那么点。