版權(quán)所有:(xiaodaoxiaodao)藍(lán)小刀
??
xiaodaoxiaodao@gmail.com
http://m.tkk7.com/xiaodaoxiaodao/archive/2007/03/24/106122.html
?
?
??
轉(zhuǎn)載請(qǐng)注明來(lái)源/作者
?
Axis
學(xué)習(xí)筆記
?
實(shí)例(參考了
axis-bin-1_4.zip
\axis-1_4\samples\userguide
中的例子)使用版本為Axis1.4,
axis-bin-1_4.zip
下載地址:
http://www.apache.org/dist/ws/axis/1_4/
?
工程axis_example目錄結(jié)構(gòu)如下:
目錄說(shuō)明如下:
jws
:存放*.jws文件
src
:java源碼
WEB-INF/classes
:java編譯后的class文件
WEB-INF/lib
:需要用到的jar包
?
Axis
支持三種web service開(kāi)發(fā)方式,分別為:
1
、Dynamic Invocation Interface ( DII)
2
、Dynamic Proxy方式
3
、Stubs方式
通過(guò)下面三個(gè)例子進(jìn)行說(shuō)明。
?
在開(kāi)始例子前,把
①
axis-bin-1_4.zip
\axis-1_4\lib
下的所有包拷貝到axis_example/WEB-INF/lib目錄下,
②
axis-bin-1_4.zip
\axis-1_4\webapps\axis\WEB-INF
下的web.xml文件拷貝到axis_example/WEB-INF目錄下。
?
實(shí)例1(DII)步驟
:
1.?
在axis_example
/src
下
新建一MyServic.java文件,內(nèi)容為:
public class MyService {
??? public String processService(String arg){
??????? return arg;
??? }
}
?
2.?
無(wú)需編譯
(編譯由axis進(jìn)行),拷貝MyServic.java到axis_example/jws目錄下,更改文件名為MyService.jws
?
3.?
在axis_example/src新建一Client.java文件,內(nèi)容為:
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceFactory;
import java.net.URL;
?
public class Client {
??? public static void main(String [] args) throws Exception {
???????
//
指出service所在URL
??????? String endpoint = "http://localhost:" + "8081" + "/axis_example/jws/MyService.jws";
???????
//
創(chuàng)建一個(gè)服務(wù)(service)調(diào)用(call)
??????? Service service = new Service();
??????? Call call = (Call) service.createCall();//
通過(guò)service創(chuàng)建call對(duì)象
???????
//
設(shè)置service所在URL
??????? call.setTargetEndpointAddress(new java.net.URL(endpoint));
???????
//
方法名(processService)與MyService.java方法名保持一致
??????? call.setOperationName("processService");
???????
// Object
數(shù)組封裝了參數(shù),參數(shù)為"This is Test!",調(diào)用processService(String arg)
??????? String ret = (String) call.invoke(new Object[]{"This is Test!"});
??????? System.out.println(ret);
??? }
}
?
4.?
axis_example
工程放入tomcat/webapps,啟動(dòng)tomcat。
?
5.?
編譯Client.java,運(yùn)行其中的main方法進(jìn)行測(cè)試,可以看到屏幕打印出:"This is Test!",可以看到axis_example/WEB-INF目錄下生jwsClasses/jws/MyService.class文件——axis會(huì)根據(jù)你訪問(wèn)時(shí)的endpoint,自動(dòng)編譯其中的*.jws文件,并置于生成的jwsClasses相應(yīng)目錄下。
(通過(guò)http://localhost:8081/axis_example/jws/MyService.jws?wsdl可以查看生成的WSDL文件——SOAP服務(wù)描述文件)
?
注1:
在上面的
new Object[]{"This is Test!"}
語(yǔ)句中,只傳遞了一個(gè)參數(shù)。如果MyServic.java中
processService(String arg)
改寫為
processService(String arg,String arg2)
你可以通過(guò)new Object[]{"test","test2"}傳遞多個(gè)參數(shù)。
?
注2:
啟動(dòng)tomcat
后控制臺(tái)出現(xiàn)下面警告:
- Unable to find required classes (javax.activation.DataHandler and javax.mail.i
nternet.MimeMultipart). Attachment support is disabled.
?
這是因?yàn)槿鄙?span lang="EN-US">activation.jar和mail.jar(本文中的實(shí)例可以忽略此警告)。
?
activation.jar
(目前版本為1.1)下載地址
http://java.sun.com/products/javabeans/jaf/downloads/index.html
mail.jar
(目前版本為1.4)下載地址
http://java.sun.com/products/javamail/downloads/
?
實(shí)例2(Dynamic Proxy)步驟
:
1.?
在axis_example
/src
下
新建一MyServiceInterface.java文件,內(nèi)容為:
import java.rmi.Remote;
import java.rmi.RemoteException;
?
public interface MyServiceInterface extends Remote {
??? public String processService(String arg) throws RemoteException;
}
編譯
MyServiceInterface.java
?
2.?
修改axis_example
/src
下
的MyServic.java文件,把類聲明
public class MyService
改為
public class MyService implements MyServiceInterface
?
3.?
無(wú)需編譯,拷貝MyServic.java到axis_example/jws目錄下,更改文件名為MyService.jws
?
4.?
更改axis_example/src/Client.java中的main方法,內(nèi)容為:
??? public static void main(String [] args) throws Exception {
??????? String wsdlUrl = "http://localhost:8081/axis_example/jws/MyService.jws?wsdl";
??????? String nameSpaceUri = "http://localhost:8081/axis_example/jws/MyService.jws";
??????? String serviceName = "MyServiceService";
????????String portName = "MyService";???? // 多謝leo提示,少了這一行代碼,現(xiàn)在加上
?
??????? ServiceFactory serviceFactory = ServiceFactory.newInstance();
??????? javax.xml.rpc.Service service = serviceFactory.createService(new URL(wsdlUrl), new QName(nameSpaceUri, serviceName));
??????? MyServiceInterface proxy = (MyServiceInterface)
??????????????? service.getPort(new QName(nameSpaceUri, portName), MyServiceInterface.class);
?
??????? System.out.println("This is " + proxy.processService("Dynamic Proxy test!"));
??? }
5.?
axis_example
工程放入tomcat/webapps,啟動(dòng)tomcat。
6.?
編譯Client.java,運(yùn)行其中的main方法進(jìn)行測(cè)試,可以看到屏幕打印出:"
This is Dynamic Proxy test!"
。
?
?
實(shí)例3(Stubs)步驟
:
1.?
在axis_example/src下新建一MyServic.java文件,內(nèi)容為:
public class MyService {
??? public String processService(String arg){
??????? return arg;
??? }
}
編譯
MyServic.java
?
2.?
在新建一deploy.wsdd(可參考
axis-bin-1_4.zip
\axis-1_4\samples
中的deploy.wsdd)文件,內(nèi)容為:
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
??????????? xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
?<service name="MyService" provider="java:RPC">
? <parameter name="className" value="MyService"/>
? <parameter name="allowedMethods" value="processService"/>
?</service>
</deployment>
?
3.?
啟動(dòng)tomcat
4.?
在axis_example/WEB-INF目錄下執(zhí)行:
java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://localhost:8081/axis_example/servlet/AxisServlet deploy.wsdd
執(zhí)行后可看到在axis_example/WEB-INF目錄下生成server-config.wsdd文件。
?
5.?
重新啟動(dòng)tomcat
,以便加載
server-config.wsdd
文件。
6.?
更改axis_example/src/Client.java中的main方法,內(nèi)容為:
??? public static void main(String [] args) throws Exception {
???????
//
指出service所在URL
??????? String endpoint = "http://localhost:" + "8081" + "/axis_example/services/MyService";
???????
//
創(chuàng)建一個(gè)服務(wù)(service)調(diào)用(call)
??????? Service service = new Service();
??????? Call call = (Call) service.createCall();//
通過(guò)service創(chuàng)建call對(duì)象
???????
//
設(shè)置service所在URL
??????? call.setTargetEndpointAddress(new java.net.URL(endpoint));
???????
//
方法名(processService)與MyService.java方法名保持一致
??????? call.setOperationName("processService");
?
???????
// Object
數(shù)組封裝了參數(shù),參數(shù)為"This is Test!",調(diào)用processService(String arg)
??????? String ret = (String) call.invoke(new Object[]{"This is Test!"});
??????? System.out.println(ret);
??? }
?
注:
在這里可以看出,
DII
方式安全性不高(url MyService.jws為axis自動(dòng)生成),且無(wú)法進(jìn)行一些復(fù)雜的配置, Dynamic Invocation Interface(DII)
和
Stubs
方式的區(qū)別主要有兩個(gè)地方:
① 兩種不同的
endpoint
,
DII
:http://localhost:8081/axis_example/jws/MyService.jws
Stubs
:http://localhost:8081/axis_example/services/MyService
?
② 兩種不同的編譯方式
DII
:根據(jù)endpoint訪問(wèn)web service時(shí),axis自動(dòng)編譯endpoint指定的*.jws文件,并放在生成的WEB-INF/jwsClasses目錄下。
Stubs
:手工編譯java文件,手工編寫server-config.wsdd配置文件(這里可以編寫deploy.wsdd,用axis提供的java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://localhost:8081/axis_example/servlet/AxisServlet deploy.wsdd
命令生成server-config.wsdd文件中的其他通用部分)
?
而Dynamic Proxy方式僅僅在DII的基礎(chǔ)上采用了代理機(jī)制,實(shí)際上和DII區(qū)別不大,。
?
7.?
編譯Client.java,運(yùn)行其中的main方法進(jìn)行測(cè)試,可以看到屏幕打印出:"
This is Dynamic Proxy test!"
(通過(guò)http://localhost:8081/axis_example/services/MyService?wsdl可以查看生成的WSDL文件——SOAP服務(wù)描述文件)
?
?
附
:
axis
提供了wsdl2java工具,web service服務(wù)器端提供了一個(gè)地址,可以訪問(wèn)到WSDL文件,wsdl2java工具格式為:java org.apache.axis.wsdl.WSDL2Java [options] WSDL-URI
?
采用DII方式,可以使用
java -Djava.ext.dirs=
E:\project\axis_example\WEB-INF\lib org.apache.axis.wsdl.WSDL2Java
http://localhost:8081/axis_example/jws/MyService.jws?wsdl
-p test.mytest -o E:\project\axis_example\src
生成相應(yīng)的客戶端java文件。
?
采用Stubs方式,可以使用
java -Djava.ext.dirs=
E:\project\axis_example\WEB-INF\lib org.apache.axis.wsdl.WSDL2Java
http://localhost:8081/axis_example/services/MyService?wsdl
-p test.mytest -o E:\project\axis_example\src
生成相應(yīng)的客戶端java文件。
參數(shù)
-p
?
指定生成的java文件包名
-o
?
指定生成的java文件輸出目錄
?
如果不指定包名,axis會(huì)根據(jù)命令參數(shù)
WSDL-URI
生成相應(yīng)的包名,如localhost\axis_example\jws\MyService_jws
?
上述命令會(huì)在
E:\project\axis_example\src\test\mytest
目錄下生成四個(gè)文件:
MyServiceSoapBindingStub.java
(相當(dāng)于上面的MyService.java)
MyService_PortType.java
(相當(dāng)于上面的MyServiceInterface.java)
MyServiceService.java/MyServiceServiceLocator.java
(Service Locator模式,隱藏了具體的業(yè)務(wù)邏輯)
?
編寫junit單元測(cè)試,在axis_example\src\test\mytest下新建一TestClient.java文件(拷貝junit.jar包到axis_example/WEB-INF目錄下),內(nèi)容為:
package test.mytest;
import junit.framework.TestSuite;
import junit.framework.TestCase;
import junit.framework.Test;
?
public class TestClient extends TestCase {
??? public TestClient(String string) {
??????? super(string);
??? }
??? public void MyServiceClient() throws Exception {
??????? MyServiceService service = new MyServiceServiceLocator();
??????? MyService_PortType client = service.getMyService() ;
??????? String ret = client.processService("This is Junit Test!");
??????? System.out.println(ret);
??? }
??? public static Test suite() {
??????? TestSuite suite = new TestSuite();
??????? suite.addTest(new TestClient("MyServiceClient"));
??????? return suite;
??? }
}
?
8.?
編譯上面四個(gè)service文件,并編譯運(yùn)行
TestClient.java
,看到屏幕打印出:"
This is Junit Test!"
?
Axis
學(xué)習(xí)筆記
.pdf
下載地址:
Axis
學(xué)習(xí)筆記
.rar
?
版權(quán)所有:(xiaodaoxiaodao)藍(lán)小刀
??
xiaodaoxiaodao@gmail.com