Axis全称Apache eXtensible Interaction System,是apache组织下的一个开源项目,用来做webservice开发的,有java版的实现,也有c++版的实现。其前身最早是IBM的SOAP4J,后来捐给Apache后改名Apache SOAP,也就是支持SOAP1.1的那个版本,到现在的AXIS已经是第三代了,AXIS对Apache SOAP的改进有很多,比如用SAX代替以前的DOM,增加对WSDL的支持,支持SOAP1.2等等。
使用axis其实很简单,在apache网站上把axis下载了后,把%AXIS_HOME%\webapps\axis整个目录拷贝到tomcat的webapps目录下面,启动tomcat,浏览器中输入http://localhost:8080/axis就可以访问了。
上面说的只是axis运行起来了,要想布置自己的webservice,axis支持两种方法:一种是用JWS(Java Web Service),好处是只要有源码比如说Hello.java,把它后缀改成jws后放到%TOMCAT_HOME%\webapps\axis目录下面,浏览器里面输入http://localhost:8080/axis/Hello.jws就可以了;第二种方法就是使用wsdd(web service deploy description),这个就是在TOMCAT_HOME%\webapps\axis\WEB-INF目录下写一个deploy.wsdd和undeploy.wsdd文件,然后通过java org.apache.axis.client.AdminClient deploy.wsdd发布,可以生成server-config.wsdd配置文件,里面是一些发布的web服务信息,而卸载web 服务就可以用org.apache.axis.client.AdminClient undeploy.wsdd来操作,客户端输入http://localhost:8080/axis/services/Hello就可以了。
下面看一个简单的例子,编写一个普通的web service例子(Hello.java)如下:
public String sayHello(String name) {
return "hello " + name;
}
}
把它改名为Hello.jws然后拷贝至%TOMCAT_HOME%\webapps\axis目录下,然后编写客户端程序如下:
try {
String endpoint = "http://localhost:8080/axis/Hello.jws";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(endpoint));
call.setOperationName("sayHello");
String result = (String) call.invoke(new Object[] { "apos" });
System.out.println(result);
} catch (Exception e) {
System.err.println(e.toString());
}
}
运行后输出“hello apos”,好了,一个简单的web service 调用成功了。
还有一种wsdd发布和调用方式,如上Hello.java,可以为其编写deploy.wsdd文件如下:
< service name ="HelloService" provider ="java:RPC" >
< parameter name ="className" value ="Hello" />
< parameter name ="allowedMethods" value ="sayHello" />
</ service >
</ deployment >
接下去启动tomcat,在DOS下切换目录到%TOMCAT_HOME%\webapps\axis\WEB-INF\目录下,执行命令行:
java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient deploy.wsdd
你会看到在当前目录下生成了server-config.wsdd配置文件。内容如下:
< deployment xmlns ="http://xml.apache.org/axis/wsdd/" xmlns:java ="http://xml.apache.org/axis/wsdd/providers/java" >
< globalConfiguration >
< parameter name ="sendMultiRefs" value ="true" />
< parameter name ="disablePrettyXML" value ="true" />
< parameter name ="adminPassword" value ="admin" />
< parameter name ="attachments.Directory" value ="D:\jakarta-tomcat-4.1.31\webapps\axis\WEB-INF\attachments" />
< parameter name ="dotNetSoapEncFix" value ="true" />
< parameter name ="enableNamespacePrefixOptimization" value ="false" />
< parameter name ="sendXMLDeclaration" value ="true" />
< parameter name ="sendXsiTypes" value ="true" />
< parameter name ="attachments.implementation" value ="org.apache.axis.attachments.AttachmentsImpl" />
< requestFlow >
< handler type ="java:org.apache.axis.handlers.JWSHandler" >
< parameter name ="scope" value ="session" />
</ handler >
< handler type ="java:org.apache.axis.handlers.JWSHandler" >
< parameter name ="scope" value ="request" />
< parameter name ="extension" value =".jwr" />
</ handler >
</ requestFlow >
</ globalConfiguration >
< handler name ="LocalResponder" type ="java:org.apache.axis.transport.local.LocalResponder" />
< handler name ="URLMapper" type ="java:org.apache.axis.handlers.http.URLMapper" />
< handler name ="Authenticate" type ="java:org.apache.axis.handlers.SimpleAuthenticationHandler" />
< service name ="AdminService" provider ="java:MSG" >
< parameter name ="allowedMethods" value ="AdminService" />
< parameter name ="enableRemoteAdmin" value ="false" />
< parameter name ="className" value ="org.apache.axis.utils.Admin" />
< namespace > http://xml.apache.org/axis/wsdd/ </ namespace >
</ service >
< service name ="Version" provider ="java:RPC" >
< parameter name ="allowedMethods" value ="getVersion" />
< parameter name ="className" value ="org.apache.axis.Version" />
</ service >
< service name ="HelloService" provider ="java:RPC" >
< parameter name ="allowedMethods" value ="sayHello" />
< parameter name ="className" value ="Hello" />
</ service >
< transport name ="http" >
< requestFlow >
< handler type ="URLMapper" />
< handler type ="java:org.apache.axis.handlers.http.HTTPAuthHandler" />
</ requestFlow >
< parameter name ="qs:list" value ="org.apache.axis.transport.http.QSListHandler" />
< parameter name ="qs:wsdl" value ="org.apache.axis.transport.http.QSWSDLHandler" />
< parameter name ="qs.list" value ="org.apache.axis.transport.http.QSListHandler" />
< parameter name ="qs.method" value ="org.apache.axis.transport.http.QSMethodHandler" />
< parameter name ="qs:method" value ="org.apache.axis.transport.http.QSMethodHandler" />
< parameter name ="qs.wsdl" value ="org.apache.axis.transport.http.QSWSDLHandler" />
</ transport >
< transport name ="local" >
< responseFlow >
< handler type ="LocalResponder" />
</ responseFlow >
</ transport >
</ deployment >
你也可以手工配置此文件以达到发布web service 的目的。
然后通过 http://localhost:8080/axis/services/HelloService?method=sayHello&name=apos 访问服务,可以看到返回soap包如下:
< soapenv:Envelope xmlns:soapenv ="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< soapenv:Body >
< sayHelloResponse soapenv:encodingStyle ="http://schemas.xmlsoap.org/soap/encoding/" >
< sayHelloReturn xsi:type ="soapenc:string" xmlns:soapenc ="http://schemas.xmlsoap.org/soap/encoding/" > hello apos </ sayHelloReturn >
</ sayHelloResponse >
</ soapenv:Body >
</ soapenv:Envelope >
呵呵,第二种方式发布并访问web service 成功了。