从无到有,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.

  • 配置环境变量:AXIS2_HOME,PATH(JDK支持,具体方法略)

  • 搭建Axis2服务环境需要Apach-tomcat支持,将axis2-1.6.0-war.zip解压后的.war文件部署到tomcat中。

  • 启动tomcat,浏览器访问: http://localhost:8080/axis2/

  • 看到Welcome,安装Axis2服务成功,欢迎页面中上的Services:服务列表,Validate:检验系统环境,Administration:Axis2安装管理控制台(登录的默认用户密码在:axis2\WEB-INF\conf\axis2.xml下)。

  • 1
    2
    < parameter  name = "userName" >admin</ parameter >
    < parameter  name = "password" >axis2</ parameter >


 2.Axis2实例开发



   下面这个例子是进入Axis2框架开发Webservice服务的Hello World程序。

   快速起步,一个简单的例子,创建个一个服务类:

 

1
2
3
4
5
6
7
8
9
package  com.axis.service;
public  class  HelloService {
     public  String sayHello(String name) {
         if  ( null  == name) {
             name =  "boby" ;
         }
         return  "Hello , "  + name;
     }
}

 

 配置services.xml文件

 

1
2
3
4
5
6
7
8
9
10
11
< 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文件的配置可也参见本文开始提到的第一篇博文。

   服务的目录结构:

   wKiom1M0DPHQEo7nAAAb4IdvS1k096.jpg

   jws2根目录下直接放服务类,META-INF下放置services.xml,然后将工程打包为jws2.jar,由于Axis2建议服务的归档文件后缀为aar,手动改后缀,然后将jws2.aar放置到tomcat的webapp下的axis2工程下的WEB-INF下的services目录下,例如我本地路径:D:\__dev\apache-tomcat-6.0.37\webapps\axis2\WEB-INF\services。

  重启Tomcat,浏览器访问http://localhost:8080/axis2/services/listServices 查看服务列表

  wKiom1M0EVqSQmOiAACI56sqVus158.jpg

  HelloService提供了一个sayHello的操作,浏览器访问:

  http://localhost:8080/axis2/services/HelloService/sayHello?name=Jack

  sayHello:方法,name参数,参数值:Jack,服务返回结果:

 

1
2
3
< ns:sayHelloResponse  xmlns:ns = "http://service.axis.com" >
< ns:return >Hello , Jack</ ns:return >
</ ns:sayHelloResponse >

 

 下面是客户端使用服务的示例代码:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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);
     }
}

 

运行上述程序可以看到控制台输出:

wKiom1M0HEHxL4KzAABJfjGFzSg264.jpg

从结果上看,第一行客户端本身的输出,第二行是服务端返回的结果打印信息。

至此展示了搭建一个简单的基于Axis2 WebService框架的服务应用实例的全过程。


  在从无到有的使用Axis2Webserivce框架做示例的过程中,反复阅读了本文上面的三篇文章,关于Webservice应用开发流程如上示,而Axis2提供的强大功能,还需要话更多的时间,精力不断学习和实践,另外要不断理解Webservice的原理,这里主要指Java开发Webservice。

 总结一下:

 1.服务程序对外提供接口,接口方法的实现通过实现类隐藏提供,这样服务端的业务发生改变不用影响到客户端的使用;

 2.服务程序对外接口要描述清晰,功能具体

 3.service.xml配置文件要组织合理,避免配置膨胀

 下面是一个服务组的services.xml文件配置:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
< 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方面经验的朋友指点帮助


转载自:http://aiilive.blog.51cto.com/1925756/1385563

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值