加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

从无到有,WebService Apache Axis2初步实践

发布时间:2020-12-16 22:00:19 所属栏目:安全 来源:网络整理
导读:?WebService说起来容易, 做起来还真有些让人一头雾水,先看了《JAVA WEB服务 构建与运行》和《JavaWebSererSOAP》大致了解了一下WebService,可是还是不得要领,想用Java做一个WebService服务的例子,然后通过其他应用来享用服务,百度,google一大堆还是获

?WebService说起来容易, 做起来还真有些让人一头雾水,先看了《JAVA WEB服务 构建与运行》和《JavaWebSererSOAP》大致了解了一下WebService,可是还是不得要领,想用Java做一个WebService服务的例子,然后通过其他应用来享用服务,百度,google一大堆还是获取到了很大帮助,比如如下博文: ?

  • http://www.voidcn.com/article/p-rnvrmutt-wo.html

    Axis2 Web服务配置文件services.xml详解

  • http://www.ibm.com/developerworks/cn/webservices/ws-apacheaxis2/??Web 服务与 Axis2 体系结构 ? ? ? ?

  • http://www.blogjava.net/nokiaguy/archive/2009/01/archive/2009/01/archive/2009/01/02/249556.html?WebService大讲堂之Axis2

? 开始学习困难重重,作文以祭之,决定先从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安装管理控制台(登录的默认用户密码在:axis2WEB-INFconfaxis2.xml下)。

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


?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文件

9
10
11
< service? name = "HelloService"? scope "application"? targetNamespace "http://service.axis.com" >
???????? description >
???????????? HelloService POJO Service
</ >
parameter? "ServiceClass" >
com.axis.service.HelloService
parameter >
operation? "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:__devapache-tomcat-6.0.37webappsaxis2WEB-INFservices。

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

??

wKiom1M0EVqSQmOiAACI56sqVus158.jpg

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

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

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

3
ns:sayHelloResponse? xmlns:ns >
ns:return >Hello,Jack</ >
ns:sayHelloResponse ?下面是客户端使用服务的示例代码:

11
12
13
14
15
16
17
18
19
20
21
22
23
24
com.axis.client;
import? javax.xml.namespace.QName;
org.apache.axis2.AxisFault;
org.apache.axis2.addressing.EndpointReference;
org.apache.axis2.client.Options;
org.apache.axis2.rpc.client.RPCServiceClient;
HelloServiceClient {
private? static? String url1 =? "http://localhost:8080/axis2/services/HelloService" ;
static? void? main(String[] args)? throws? AxisFault {
RPCServiceClient client;
client =? new? RPCServiceClient();
Options options = client.getOptions();
System.out.println( "======sayHello========" );
EndpointReference endpoint =? EndpointReference(url1);
options.setTo(endpoint);
QName qname =? QName( "http://service.axis.com" ,? "sayHello" );
Object[] methodArgs =? Object[] {? "Tom"? };
???????? @SuppressWarnings ( "rawtypes" )
Class[] returnType =? Class[] { String. };
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文件配置:

24
25
26
27
28
serviceGroup
???? "MoneyService"? "application" >
MoneyService POJO Service
>
com.axis.service.MoneyService
"getMoney" >
/>
>
>
"WriteFileService"? "application"
???????? >
>
h3 >这是一个提供文件写入的服务,客户端将本地文件写入远程服务器</ >
>
>
com.axis.service.FileTransportImpl
>
messageReceivers >
mep "http://www.w3.org/2004/08/wsdl/in-out"
???????????????? />
"http://www.w3.org/2004/08/wsdl/in-only"
"org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"? />
>
>
? 4.Webservice真应了《JavaWebSererSOAP》作者所言,说起来容易,实践起来真心不易。

? ?初次接触Webservice开发,路还很长。

? ?PS:希望有Java开发Webservice方面经验的朋友指点帮助


转载自:http://www.voidcn.com/article/p-pckxgtqz-kn.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

相关内容
推荐文章
    热点阅读