webService总结(四)——使用axis2发布和调用webService
准备工作 Axis2 官网 http://axis.apache.org/? 下载axis2相关资料 其中 axis2-1.6.2-bin.zip文件中包含了Axis2中所有的jar文件,axis2-1.6.2-war.zip文件用于将WebService发布到Web容器中。最后两个是axis2在eclipse中的插件。 ? 大概说说这几个文件如何使用。 1、解压axis2-1.6.2-bin.zip到任意目录。然后在eclipse中按下图配置。 2、将axis2-1.6.2-war.zip文件解压到任意目录,将目录中的axis2.war文件放到<Tomcat安装目录>webapps目录中,并启动Tomcat,在浏览器地址栏中输入如下的URL: http://localhost:8080/axis2/,如看到axis2的主页面则安装成功。如下图: 3、eclipse中安装那两个axis2 的插件,具体安装步骤这里就不说了,安装后eclipse中 file——new——others,如果显示如下画面,说明安装成功。 ? 使用axis2发布webService eclipse中编写一个java? POJO类——HelloWorldServerImp,代码如下:
package com.test.server; public class HelloWorldServerImp { public String sayHello(String username) { return username+" : HelloWorld"; } }我能告诉你编码工作已经全部结束了吗? 看看发布过程: File ——》new ——》Axis2? Service archive ? ? 这个直接略过 ? ? 输出位置直接选到Tomcat中如下目录,finish即可自动发布。 这时你会看到对应的目录下多了如下文件 访问网址:http://localhost:8080/axis2/services/listServices? 即可看到我们发布的webService。 至此,发布成功。 ? 客户端调用webService 使用axis2实现,代码如下 WsClient代码:
package com.test.server; import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; public class WsClient { private RPCServiceClient serviceClient; private Options options; private EndpointReference targetEPR; public WsClient(String endpoint) throws AxisFault { serviceClient = new RPCServiceClient(); options = serviceClient.getOptions(); targetEPR = new EndpointReference(endpoint); options.setTo(targetEPR); } public Object[] invokeOp(String targetNamespace,String opName,Object[] opArgs,Class<?>[] opReturnType) throws AxisFault,ClassNotFoundException { // 设定操作的名称 QName opQName = new QName(targetNamespace,opName); // 操作需要传入的参数已经在参数中给定,这里直接传入方法中调用 return serviceClient.invokeBlocking(opQName,opArgs,opReturnType); } /** * @param args * @throws AxisFault * @throws ClassNotFoundException */ public static void main(String[] args) throws AxisFault,ClassNotFoundException { //这个是wsdl地址 final String endPointReference = "http://localhost:8080/axis2/services/HelloWorldServer"; final String targetNamespace = "http://server.test.com"; WsClient client = new WsClient(endPointReference); String opName = "sayHello"; Object[] opArgs = new Object[] { "haitao" }; Class<?>[] opReturnType = new Class[] { String[].class }; Object[] response = client.invokeOp(targetNamespace,opName,opReturnType); System.out.println(((String[]) response[0])[0]); } } 当然,也可以使用cxf调用,代码跟前几篇博客的客户端一样:
package com.test.client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; public class HelloWorldClient { public static void main(String[] args) { JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:9000/HelloService?wsdl"); Object[] objects; try { objects = client.invoke("sayHello","haitao"); //输出调用结果 System.out.println(objects[0].toString()); } catch (Exception e) { e.printStackTrace(); } } } 至此, 使用 axis2 发布和调用 webService结束。没接触axis2之前,感觉这东西挺神秘的,等你真正实现了,你会发现,它也不过如此。不过,axis2的东西远非这点,继续学习吧。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |