从无到有,WebService Apache Axis2初步实践
? ?WebService说起来容易, 做起来还真有些让人一头雾水,先看了《JAVA WEB服务 构建与运行》和《JavaWebSererSOAP》大致了解了一下WebService,可是还是不得要领,想用Java做一个WebService服务的例子,然后通过其他应用来享用服务,百度,google一大堆还是获取到了很大帮助,比如如下博文: ?
? 开始学习困难重重,作文以祭之,决定先从Axis2框架开始,以实践驱动对Webservice的理解和开发的学习。 ?1.Axis2环境搭建 ? ?下载: axis2-1.6.0-bin.zip ?axis2-1.6.0-war.zip ? ?官方网站:http://axis.apache.org/axis2/java/core/download.cgi ? ?如果下载axis2-1.6.0-src.zip,需要Ant,Maven支持来构建bin.
?2.Axis2实例开发 ? ?下面这个例子是进入Axis2框架开发Webservice服务的Hello World程序。 ? ?快速起步,一个简单的例子,创建个一个服务类: ? package com.axis.service; public class HelloService { public String sayHello(String name) { if (null == name) { name = "boby"; } return "Hello," + name; } } ? ?配置services.xml文件 ? <service name="HelloService" scope="application" targetNamespace="http://service.axis.com"> <description> HelloService POJO Service </description> <parameter name="ServiceClass"> com.axis.service.HelloService </parameter> <operation name="sayHello"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> </service> ? ? HelloService实现服务要提供的功能,services.xml配置对服务进行描述,最后部署服务。 ? 关于services.xml文件的配置可也参见本文开始提到的第一篇博文。 ? ?服务的目录结构: ? ? ? ?jws2根目录下直接放服务类,META-INF下放置services.xml,然后将工程打包为jws2.jar,由于Axis2建议服务的归档文件后缀为aar,手动改后缀,然后将jws2.aar放置到tomcat的webapp下的axis2工程下的WEB-INF下的services目录下,例如我本地路径:D:__devapache-tomcat-6.0.37webappsaxis2WEB-INFservices。 ? 重启Tomcat,浏览器访问http://localhost:8080/axis2/services/listServices 查看服务列表 ? ? HelloService提供了一个sayHello的操作,浏览器访问: ? http://localhost:8080/axis2/services/HelloService/sayHello?name=Jack ? sayHello:方法,name参数,参数值:Jack,服务返回结果: ? <ns:sayHelloResponse xmlns:ns="http://service.axis.com"> <ns:return>Hello,Jack</ns:return> </ns:sayHelloResponse> ? ?下面是客户端使用服务的示例代码: ? package com.axis.client; 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 HelloServiceClient { private static String url1 = "http://localhost:8080/axis2/services/HelloService"; public static void main(String[] args) throws AxisFault { RPCServiceClient client; client = new RPCServiceClient(); Options options = client.getOptions(); System.out.println("======sayHello========"); EndpointReference endpoint = new EndpointReference(url1); options.setTo(endpoint); QName qname = new QName("http://service.axis.com","sayHello"); Object[] methodArgs = new Object[] { "Tom" }; @SuppressWarnings("rawtypes") Class[] returnType = new Class[] { String.class }; String result = (String) client.invokeBlocking(qname,methodArgs,returnType)[0]; System.out.println(result); } } ? 运行上述程序可以看到控制台输出:
从结果上看,第一行客户端本身的输出,第二行是服务端返回的结果打印信息。 至此展示了搭建一个简单的基于Axis2 WebService框架的服务应用实例的全过程。 ? 在从无到有的使用Axis2Webserivce框架做示例的过程中,反复阅读了本文上面的三篇文章,关于Webservice应用开发流程如上示,而Axis2提供的强大功能,还需要话更多的时间,精力不断学习和实践,另外要不断理解Webservice的原理,这里主要指Java开发Webservice。 ?总结一下: ?1.服务程序对外提供接口,接口方法的实现通过实现类隐藏提供,这样服务端的业务发生改变不用影响到客户端的使用; ?2.服务程序对外接口要描述清晰,功能具体 ?3.service.xml配置文件要组织合理,避免配置膨胀 ?下面是一个服务组的services.xml文件配置: ? <serviceGroup> <service name="MoneyService" scope="application"> <description> MoneyService POJO Service </description> <parameter name="ServiceClass"> com.axis.service.MoneyService </parameter> <operation name="getMoney"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> </service> <service name="WriteFileService" scope="application" targetNamespace="http://service.axis.com"> <description> <h3>这是一个提供文件写入的服务,客户端将本地文件写入远程服务器</h3> </description> <parameter name="ServiceClass"> com.axis.service.FileTransportImpl </parameter> <messageReceivers> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> </messageReceivers> </service> </serviceGroup> ? 4.Webservice真应了《JavaWebSererSOAP》作者所言,说起来容易,实践起来真心不易。 ? ?初次接触Webservice开发,路还很长。 ? ?PS:希望有Java开发Webservice方面经验的朋友指点帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Cannot find a valid baseurl for repo: base
- Scala函数用于获取大小为k的所有已排序子集
- angularjs – 在Jasmine 2.0的异步测试中获取“$
- 方法参数类型忽略Scala不变泛型类型参数,具体取决
- Bootstrap 之 Metronic 模板的学习之路 - (7)G
- Docker ip for windows
- angularjs – ionic / angular 1.3 – 当视图中存
- twitter-bootstrap – 如何在多个属性上搜索对象
- Angular 2.x 从0到1 (一)史上最简单的Angular2教
- angularjs – ngOptions导致选项具有错误的值