axis WebService开发指南.docx

上传人:b****2 文档编号:3506290 上传时间:2023-05-05 格式:DOCX 页数:81 大小:133.75KB
下载 相关 举报
axis WebService开发指南.docx_第1页
第1页 / 共81页
axis WebService开发指南.docx_第2页
第2页 / 共81页
axis WebService开发指南.docx_第3页
第3页 / 共81页
axis WebService开发指南.docx_第4页
第4页 / 共81页
axis WebService开发指南.docx_第5页
第5页 / 共81页
axis WebService开发指南.docx_第6页
第6页 / 共81页
axis WebService开发指南.docx_第7页
第7页 / 共81页
axis WebService开发指南.docx_第8页
第8页 / 共81页
axis WebService开发指南.docx_第9页
第9页 / 共81页
axis WebService开发指南.docx_第10页
第10页 / 共81页
axis WebService开发指南.docx_第11页
第11页 / 共81页
axis WebService开发指南.docx_第12页
第12页 / 共81页
axis WebService开发指南.docx_第13页
第13页 / 共81页
axis WebService开发指南.docx_第14页
第14页 / 共81页
axis WebService开发指南.docx_第15页
第15页 / 共81页
axis WebService开发指南.docx_第16页
第16页 / 共81页
axis WebService开发指南.docx_第17页
第17页 / 共81页
axis WebService开发指南.docx_第18页
第18页 / 共81页
axis WebService开发指南.docx_第19页
第19页 / 共81页
axis WebService开发指南.docx_第20页
第20页 / 共81页
亲,该文档总共81页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

axis WebService开发指南.docx

《axis WebService开发指南.docx》由会员分享,可在线阅读,更多相关《axis WebService开发指南.docx(81页珍藏版)》请在冰点文库上搜索。

axis WebService开发指南.docx

axisWebService开发指南

hoojo

授之以鱼,不如授之以渔!

希望能给广大IT朋友带来一些帮助

IT达人

自由IT职业

506637315

2010-12-19

Axis1.x

一、搭建简单的axisweb服务

1、在官方网站下载axis的工程(这个等下就有用的)和源码、jar包等,下载地址是:

2、解压下载的工程或源码(两个中任意一个都可以),解压axis-bin-1.4可以看到大致目录是这样的:

docs是文档、lib是jar包、sample是示例、xmls是当前工程所需的xml、webapps是当前工程的webroot目录;

我们打开webapps目录就可以看到一个axis的文件夹,这个文件夹里面有WEB-INF文件夹和一些页面,将axis复制到你的tomcat的webapps目录下。

然后启动tomcat服务,访问http:

//localhost:

8080/axis/,看到下面的解码就说明部署成功了:

以后我们将和这个工程不离不弃,它将在我们的axis1.x的webService中发挥很大的作用!

3、创建我们自己的web工程,这里我新建的AxisWebService;创建好工程后,将刚才解压的axis-bin中的lib的jar包copy到当前工程的lib中;

axis-ant.jar

axis.jar

commons-discovery-0.2.jar

commons-logging-1.0.4.jar

jaxrpc.jar

log4j-1.2.8.jar

saaj.jar

wsdl4j-1.5.1.jar

activation-1.1.jar

mail-1.4.jar

创建webService类文件,代码如下:

packagecom.hoo.service;

/**

*function:

jws的axisWebService

*@authorhoojo

*@createDateDec15,201017:

03:

49PM

*@fileHelloWorldService.java

*@packagecom.hoo.service

*@projectAxisWebService

*@blog

*@emailhoojo_@

*@version1.0

*/

publicclassHelloWorldService{

publicStringsayHello(Stringname,intage){

returnname+"say:

helloworld!

[axis]myageis"+age;

}

}

4、复制HelloWorldService.java到我们刚才复制的axis文件夹下即可;也就是tomcat下的webapps下的axis下即可;注意:

还有重要的一般就是要将这个java文件中的包名去掉,并且将这个文件重命名为HelloWorldService.jws;如果带包名的话,请求后编译的class将会在包路径下,这样我们在全球当前jws的时候就会出现找不到class,详细的你可以到发布在tomcat下的工程看看WEB-INF目录下的jwsClass就一目了然了。

上面的工作完成后,启动tomcat服务器,访问http:

//localhost:

8080/axis/HelloWorldService.jws

你会看到:

ThereisaWebServicehere

ClicktoseetheWSDL

如果你和我看到的是一样的,就证明你已经成功的部署了一个axis1.x的webService。

然后我们点击下就可以看到wsdl的xml文件了,内容如下:

xmlversion="1.0"encoding="UTF-8"?

>

-

definitionstargetNamespace="http:

//localhost:

8080/axis/HelloWorldService.jws"xmlns:

apachesoap="http:

//xml.apache.org/xml-soap"xmlns:

impl="http:

//localhost:

8080/axis/HelloWorldService.jws"xmlns:

intf="http:

//localhost:

8080/axis/HelloWorldService.jws"xmlns:

soapenc="http:

//schemas.xmlsoap.org/soap/encoding/"xmlns:

wsdl="http:

//schemas.xmlsoap.org/wsdl/"xmlns:

wsdlsoap="http:

//schemas.xmlsoap.org/wsdl/soap/"xmlns:

xsd="http:

//www.w3.org/2001/XMLSchema">

-

--

WSDLcreatedbyApacheAxisversion:

1.4

BuiltonApr22,2006(06:

55:

48PDT)

 -->

-

messagename="sayHelloResponse">

 

partname="sayHelloReturn"type="xsd:

string"/>

 

message>

-

messagename="sayHelloRequest">

 

partname="name"type="xsd:

string"/>

 

partname="age"type="xsd:

int"/>

 

message>

-

portTypename="HelloWorldService">

-

operationname="sayHello"parameterOrder="nameage">

 

inputmessage="impl:

sayHelloRequest"name="sayHelloRequest"/>

 

outputmessage="impl:

sayHelloResponse"name="sayHelloResponse"/>

 

operation>

 

portType>

-

bindingname="HelloWorldServiceSoapBinding"type="impl:

HelloWorldService">

 

bindingstyle="rpc"transport="http:

//schemas.xmlsoap.org/soap/http"/>

-

operationname="sayHello">

 

operationsoapAction=""/>

-

inputname="sayHelloRequest">

 

bodyencodingStyle="http:

//schemas.xmlsoap.org/soap/encoding/"namespace="http:

//DefaultNamespace"use="encoded"/>

 

input>

-

outputname="sayHelloResponse">

 

bodyencodingStyle="http:

//schemas.xmlsoap.org/soap/encoding/"namespace="http:

//localhost:

8080/axis/HelloWorldService.jws"use="encoded"/>

 

output>

 

operation>

 

binding>

-

servicename="HelloWorldServiceService">

-

portbinding="impl:

HelloWorldServiceSoapBinding"name="HelloWorldService">

 

addresslocation="http:

//localhost:

8080/axis/HelloWorldService.jws"/>

 

port>

 

service>

 

definitions>

分析下wsdl的xml文件内容:

targetNamespace=http:

//localhost:

8080/axis/HelloWorldService.jws

是我们部署的webservice命名空间,也就是我们访问的webService路径。

messagename="sayHelloResponse">

 

partname="sayHelloReturn"type="xsd:

string"/>

 

message>

是返回值的信息,sayHelloResponse代表响应,即返回值,type是返回值的类型

messagename="sayHelloRequest">

 

partname="name"type="xsd:

string"/>

 

partname="age"type="xsd:

int"/>

 

message>

请求方法参数信息,sayHelloRequest即请求,part是参数parameter,type是参数的类型

portTypename="HelloWorldService">

operationname="sayHello"parameterOrder="nameage">

 

inputmessage="impl:

sayHelloRequest"name="sayHelloRequest"/>

 

outputmessage="impl:

sayHelloResponse"name="sayHelloResponse"/>

 

operation>

 

portType>

portType的name是当前webService的名称,operation是一个操作,即可以调用的方法。

name就是方法名称了,parameterOrder是参数,input输入即传入参数,output输出即返回的值;

servicename="HelloWorldServiceService">

portbinding="impl:

HelloWorldServiceSoapBinding"name="HelloWorldService">

 

addresslocation="http:

//localhost:

8080/axis/HelloWorldService.jws"/>

 

port>

 

service>

webService的名称和绑定的信息,以及访问的url地址。

5、下面编写客户端代码

代码如下:

packagecom.hoo.client;

importjava.rmi.RemoteException;

importjavax.xml.namespace.QName;

importjavax.xml.rpc.ServiceException;

importorg.apache.axis.client.Call;

importorg.apache.axis.client.Service;

publicclassHelloWorldClient{

/**

*function:

jwsaxisWebService客户端

*@authorhoojo

*@createDate2010-12-15下午05:

10:

28

*@paramargs

*@throwsServiceException

*@throwsRemoteException

*/

publicstaticvoidmain(String[]args)throwsServiceException,RemoteException{

//webService访问地址

//Stringurl="http:

//localhost:

8080/axis/HelloWorldService.jws";

Stringurl="http:

//localhost:

8080/AxisWebService/HelloWorldService.jws";

//创建服务

Serviceservice=newService();

//创建调用句柄

Callcall=(Call)service.createCall();

//设置请求地址

call.setTargetEndpointAddress(url);

/**

*设置调用的方法和方法的命名空间;

*因为这里是手动发布到webroot目录下的,所以命名空间和请求地址一致

*当然null也可以,因为本身它就没有设置命名空间,一般方法的命名空间是

*包名倒写组成,如com.hoo.service,ns=

*/

call.setOperationName(newQName(null,"sayHello"));

/**

*用call调用sayHello方法,设置请求的参数,返回的就是返回值了

*/

Stringresult=(String)call.invoke(newObject[]{"jack",99});

System.out.println(result);

}

}

分析上面的代码

url是根据xml文件中的wsdlsoap:

addresslocation的信息得到的,命名空间和方法名称是根据

operationname="sayHello">

operationsoapAction=""/>

-

inputname="sayHelloRequest">

bodyencodingStyle="http:

//schemas.xmlsoap.org/soap/encoding/"namespace="http:

//DefaultNamespace"use="encoded"/>

input>

-

outputname="sayHelloResponse">

bodyencodingStyle="http:

//schemas.xmlsoap.org/soap/encoding/"namespace="http:

//localhost:

8080/axis/HelloWorldJWS.jws"use="encoded"/>

output>

的信息得到的,而请求参数和返回值的详细信息是在

messagename="sayHelloRequest">

partname="name"type="xsd:

string"/>

partname="age"type="xsd:

int"/>

message>

-

messagename="sayHelloResponse">

partname="sayHelloReturn"type="xsd:

string"/>

message>

-

portTypename="HelloWorldJWS">

-

operationname="sayHello"parameterOrder="nameage">

inputmessage="impl:

sayHelloRequest"name="sayHelloRequest"/>

outputmessage="impl:

sayHelloResponse"name="sayHelloResponse"/>

operation>

portType>

里可以很详细的看到。

至于代码的call.invoke是java中反射机制,不懂的建议看看jdk文档java.lang.reflect包下的内容。

运行上面的代码就可以看到控制台输出:

jacksay:

helloworld!

[axis]myageis99

好了,axis的就完成了,下面我们不用官方的axis的工程,我们写一个自己的AxisWebService工程,然后发布的tomcat的webapps中看看。

6、刚才copy了lib下的jar包,现在要copy下web.xml中的内容,去掉里面的AdminServlet这个配置,其他的都可保留。

然后像刚才一样,将HelloWorldService.java复制到webroot目录下,去掉包名,并且修改后缀为HelloWorldService.jws即可。

(如果有兴趣可以看看,发布在tomcat目录下的当前工程的web-inf目录,看看里面是否多了些东西)最后发布当前web工程,访问http:

//localhost:

8080/AxisWebService/HelloWorldService.jws,如果看到和刚才一样的界面,证明你快成功了。

点击链接看到wsdl的xml就成功了。

好了,还没有完。

看看web.xml中的配置,你大概就知道为什么了。

web.xml中最主要的文件就是org.apache.axis.transport.http.AxisServlet,它就是webService的中央控制器;即配置jws的后缀也在web.xml中定义的,在看看还有services/*,这就表明上面的访问路径也可以是这样的:

http:

//localhost:

8080/AxisWebService/services/HelloWorldService

当然如果要这样写就需要用wsdd的发布方式,详细请看下文!

参考文档:

http:

//ws.apache.org/axis/java/user-guide.html

二、用wsdd(WebServicesDeploymentDescriptor)方式发布WebService

wsdd方法比jws方法要稍微复杂些,但一定程度上比jws发布的方法要灵活。

1、首先在AxisWebService工程创建一个java类,里面写简单的2个方法getName、getAge,代码如下:

packagecom.hoo.service;

/**

*function:

wsdd发布模式的axisWebService

*@authorhoojo

*@createDateDec15,201017:

24:

35PM

*@fileHelloWorldWSDD.java

*@packagecom.hoo.service

*@projectAxisWebService

*@blog

*@emailhoojo_@

*@version1.0

*/

publicclassHelloWorldWSDD{

publicStringgetName(Stringname){

return"yourname:

"+name;

}

publicintgetAge(intage){

returnage+10;

}

}

2、如果用wsdd方法首先需要定制我们的wsddxml文件,这里命名为deploy.wsdd,创建在当前web工程的WEB-INF目录下,代码如下:

xmlversion="1.0"encoding="UTF-8"?

>

//xml.apache.org/axis/wsdd/"

xmlns:

java="http:

//xml.apache.org/axis/wsdd/providers/java">

RPC">

--*代表所有的方法都暴露-->

service标签代表一个WebService服务,HelloWorldWSDD就是当前WebService的名称;provider是java的WebService类型,分别有:

RPC、Document、Wrapped、Message、EJB、RMI;有兴趣的可以看看org.apache.axis.providers.java包下面的WebService的实现类或是文档;

parameter的参数className代表当前WebService的class类路径;

allowedMethods代表暴露的方法,那些方法在客户端可以调用;

这个是当前WebService的作用域,它有3个值,分别是:

request、session、application。

request代表为每个WebServiceSOAP的请求都产生一个服务对象,和Spring的scope很像,在服务请求频繁的话会消耗很多资源。

session是给每个

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2