这节我们就来写一个客户端来调用一下这个服务。主要关注一下如何用elispe的axis2的插件来生成stub代码。
1.在eclispe 的package Explorer 中点击右键,在菜单中选择新建--->other...----->Axis2 Code Generator
2.点击next,进入下一个页面,选择从wsdl文件来产生java文件。
3. 点击next,然后选择wsdl文件,注意此处要填写上一节我们生成的WSDL地址
4.点击next,进入设置页面,这里我们就用默认的设置。
5. 点击next,选择输出文件的路径。
6.点击next,如果看到这个页面,恭喜你已经生成代码成功。
如果在MyEclipse6.5中报错:
An error ocurred while completing process
-java.lang.reflect.InvocationTargetException
主要的问题在于 Code Generator plugin 内缺少了 backport-util-concurrent-3.1.jar 包和geronimo-stax-api_1.0_spec-1.0.1.jar 包。
[解决方案] :
(1).关闭 Eclipse
(2).copy %AXIS2_HOME%/lib/ 下的
backport-util-concurrent-3.1.jar 和 geronimo-stax-api_1.0_spec-1.0.1.jar
复制到 MyEclipse 6.5/eclipse/plugins/Axis2_Codegen_Wizard_1.3.0/lib 文件夹下。
(3).注册此 jar 包:
修改MyEclipse 6.5/eclipse/plugins/Axis2_Codegen_Wizard_1.3.0/plugin.xml 文件
在 <runtime> 內加入下面的字串
<library name="lib/geronimo-stax-api_1.0_spec-1.0.1.jar">
<export name="*"/>
</library>
<library name="lib/backport-util-concurrent-3.1.jar">
<export name="*"/>
</library>
(4),除了上面步骤还要:
(4.1). 把Axis2_Codegen_wizard_1.3.0(eclipse/plugins/Axis2_Codegen_wizard_1.3.0)的名字改成Axis2_Codegen_wizard_1.4.0
(4.2). 在plugin.xml中在<plugin>中 把Axis2_Codegen_wizard的version="1.3.0"改成version="1.4.0"
到plugin.xml文件中,保存后重新启动Eclipse即可。
7.在package Explorer中刷新一下项目,然后你发现出现2个新的文件SimpleServerStub和SimpleServerCallbackHandler 。打开SimpleServerStub你会惊喜的发现。著名的小红叉一个接一个的
这是因为没有axis2的类包。我们可以在下载的axis2-1.4.1-bin中找到lib包,把其中的jar都加入我们的工程中。
然后重新编译一下工程,这时我们发现SimpleServerStub还是有几个小红叉。这个是因为这个插件有个小bug。
生成的代码没有实现序列化方法。我们可以自己来加上,在小红叉上点一下,弹出一个小菜单,选择
Add unimplemented methods .
8. 调用发布的Web Service 服务
java代码
- /**
- * 调用发布的服务。
- *
- */
- public class SimpleClient {
- public static void main(String[] args) throws Exception{
- //初始化桩文件
- SimpleServerStub stub = new SimpleServerStub();
- //初始化SimpleMethod方法。
- SimpleServerStub.SimpleMethod request = new SimpleServerStub.SimpleMethod();
- //调用simpleMethod的setName方法。
- request.setName("zt");
- //
- System.out.println(stub.simpleMethod(request).get_return());
- }
- }
如果一切正常,你就会看到结果
log4j:WARN No appenders could be found for logger (org.apache.axis2.description.AxisService).
log4j:WARN Please initialize the log4j system properly.
ztSay this is a Simple method ^-^。