1、在官方網站下載axis的工程(這個等下就有用的)和源碼、jar包等,是:
http://labs.renren.com/apache-mirror//ws/axis/1_4/
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類檔案,代碼如下:
package com.hoo.service;
/**
* <b>function:</b>jws的axis WebService
* @author hoojo
* @createDate Dec 15, 2010 17:03:49 PM
* @file HelloWorldService.java
* @package com.hoo.service
* @project AxisWebService
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
publicclass HelloWorldService {
public String sayHello(String name, int age) {
return name +" say : hello world! [axis] my age is "+ 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
你會看到:
There is a Web Service here
Click to see the WSDL
如果你和我看到的是一樣的,就證明你已經成功的部署了一個axis1.x的webService。然後我們點擊下就可以看到wsdl的xml檔案了,內容如下:
<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions targetNamespace="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">
- <!--
WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)
-->
- <wsdl:message name="sayHelloResponse">
<wsdl:part name="sayHelloReturn" type="xsd:string"/>
</wsdl:message>
- <wsdl:message name="sayHelloRequest">
<wsdl:part name="name" type="xsd:string"/>
<wsdl:part name="age" type="xsd:int"/>
</wsdl:message>
- <wsdl:portType name="HelloWorldService">
- <wsdl:operation name="sayHello" parameterOrder="name age">
<wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest"/>
<wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse"/>
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="HelloWorldServiceSoapBinding" type="impl:HelloWorldService">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
- <wsdl:operation name="sayHello">
<wsdlsoap:operation soapAction=""/>
- <wsdl:input name="sayHelloRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://DefaultNamespace" use="encoded"/>
</wsdl:input>
- <wsdl:output name="sayHelloResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/axis/HelloWorldService.jws" use="encoded"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="HelloWorldServiceService">
- <wsdl:port binding="impl:HelloWorldServiceSoapBinding" name="HelloWorldService">
<wsdlsoap:address location="http://localhost:8080/axis/HelloWorldService.jws"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
分析下wsdl的xml檔案內容:
targetNamespace=http://localhost:8080/axis/HelloWorldService.jws
是我們部署的webservice命名空間,也就是我們訪問的webService路徑。
<wsdl:message name="sayHelloResponse">
<wsdl:part name="sayHelloReturn" type="xsd:string" />
</wsdl:message>
是傳回值的資訊,sayHelloResponse代表響應,即傳回值,type是傳回值的類型
<wsdl:message name="sayHelloRequest">
<wsdl:part name="name" type="xsd:string" />
<wsdl:part name="age" type="xsd:int" />
</wsdl:message>
要求方法參數資訊,sayHelloRequest即請求,part是參數parameter,type是參數的類型
<wsdl:portType name="HelloWorldService">
<wsdl:operation name="sayHello" parameterOrder="name age">
<wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest" />
<wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse" />
</wsdl:operation>
</wsdl:portType>
portType的name是當前webService的名稱,operation是一個操作,即可以調用的方法。name就是方法名稱了,parameterOrder是參數,input輸入即傳入參數,output輸出即返回的值;
<wsdl:service name="HelloWorldServiceService">
<wsdl:port binding="impl:HelloWorldServiceSoapBinding" name="HelloWorldService">
<wsdlsoap:address location="http://localhost:8080/axis/HelloWorldService.jws" />
</wsdl:port>
</wsdl:service>
webService的名稱和綁定的資訊,以及訪問的url地址。
5、下面編寫用戶端代碼
代碼如下:
package com.hoo.client;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
publicclass HelloWorldClient {
/**
* <b>function:</b>jws axis WebService用戶端
* @author hoojo
* @createDate 2010-12-15 下午05:10:28
* @param args
* @throws ServiceException
* @throws RemoteException
*/
publicstaticvoid main(String[] args) throws ServiceException, RemoteException {
//webService訪問地址
//String url = "http://localhost:8080/axis/HelloWorldService.jws";
String url ="http://localhost:8080/AxisWebService/HelloWorldService.jws";
//建立服務
Service service =new Service();
//建立調用控制代碼
Call call = (Call) service.createCall();
//佈建要求地址
call.setTargetEndpointAddress(url);
/**
* 設定調用的方法和方法的命名空間;
* 因為這裡是手動發布到webroot目錄下的,所以命名空間和請求地址一致
* 當然null也可以,因為本身它就沒有設定命名空間,一般方法的命名空間是
* 包名倒寫組成,如com.hoo.service,ns=http://service.hoo.com
*/
call.setOperationName(new QName(null, "sayHello"));
/**
* 用call調用sayHello方法,佈建要求的參數,返回的就是傳回值了
*/
String result = (String) call.invoke(new Object[] { "jack", 99 });
System.out.println(result);
}
}
分析上面的代碼
url是根據xml檔案中的wsdlsoap:address location的資訊得到的,命名空間和方法名稱是根據
<wsdl:operation name="sayHello">
<wsdlsoap:operation soapAction=""/>
- <wsdl:input name="sayHelloRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://DefaultNamespace" use="encoded"/>
</wsdl:input>
- <wsdl:output name="sayHelloResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/axis/HelloWorldJWS.jws" use="encoded"/>
</wsdl:output>
的資訊得到的,而請求參數和傳回值的詳細資料是在
<wsdl:message name="sayHelloRequest">
<wsdl:part name="name" type="xsd:string"/>
<wsdl:part name="age" type="xsd:int"/>
</wsdl:message>
- <wsdl:message name="sayHelloResponse">
<wsdl:part name="sayHelloReturn" type="xsd:string"/>
</wsdl:message>
- <wsdl:portType name="HelloWorldJWS">
- <wsdl:operation name="sayHello" parameterOrder="name age">
<wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest"/>
<wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse"/>
</wsdl:operation>
</wsdl:portType>
裡可以很詳細的看到。
至於代碼的call.invoke是java中反射機制,不懂的建議看看jdk文檔java.lang.reflect包下的內容。
運行上面的代碼就可以看到控制台輸出:
jack say : hello world! [axis] my age is 99
好了,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