axis WebService开发指南.docx
《axis WebService开发指南.docx》由会员分享,可在线阅读,更多相关《axis WebService开发指南.docx(81页珍藏版)》请在冰豆网上搜索。
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是给每个