|
版權(quán)所有:(xiaodaoxiaodao)藍小刀 xiaodaoxiaodao@gmail.com http://www./xiaodaoxiaodao/archive/ 轉(zhuǎn)載請注明來源/作者 Axis 學(xué)習(xí)筆記 實例(參考了 axis-bin-1_4.zip \axis-1_4\samples\userguide 中的例子)使用版本為Axis1.4, axis-bin-1_4.zip 下載地址: 工程axis_example目錄結(jié)構(gòu)如下:
jws :存放*.jws文件 src :java源碼 WEB-INF/classes :java編譯后的class文件 WEB-INF/lib :需要用到的jar包 Axis 支持三種web service開發(fā)方式,分別為: 1 、Dynamic Invocation Interface ( DII) 2 、Dynamic Proxy方式 3 、Stubs方式 通過下面三個例子進行說明。 在開始例子前,把 ① 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目錄下。 實例1(DII)步驟 : 1. 在axis_example /src 下 新建一MyServic.java文件,內(nèi)容為: public class MyService { public String processService(String arg){ return arg; } } 2. 無需編譯 (編譯由axis進行),拷貝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)建一個服務(wù)(service)調(diào)用(call) Service service = new Service(); Call call = (Call) service.createCall();// 通過service創(chuàng)建call對象 // 設(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,啟動tomcat。 5. 編譯Client.java,運行其中的main方法進行測試,可以看到屏幕打印出:"This is Test!",可以看到axis_example/WEB-INF目錄下生jwsClasses/jws/MyService.class文件——axis會根據(jù)你訪問時的endpoint,自動編譯其中的*.jws文件,并置于生成的jwsClasses相應(yīng)目錄下。 (通過http://localhost:8081/axis_example/jws/MyService.jws?wsdl可以查看生成的WSDL文件——SOAP服務(wù)描述文件) 注1: 在上面的 new Object[]{"This is Test!"} 語句中,只傳遞了一個參數(shù)。如果MyServic.java中 processService(String arg) 改寫為 processService(String arg,String arg2) 你可以通過new Object[]{"test","test2"}傳遞多個參數(shù)。 注2: 啟動tomcat 后控制臺出現(xiàn)下面警告: - Unable to find required classes (javax.activation.DataHandler and javax.mail.i nternet.MimeMultipart). Attachment support is disabled. 這是因為缺少activation.jar和mail.jar(本文中的實例可以忽略此警告)。 activation.jar (目前版本為1.1)下載地址 http://java./products/javabeans/jaf/downloads/index.html mail.jar (目前版本為1.4)下載地址 http://java./products/javamail/downloads/ 實例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. 無需編譯,拷貝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"; 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,啟動tomcat。 6. 編譯Client.java,運行其中的main方法進行測試,可以看到屏幕打印出:" This is Dynamic Proxy test!" 。 實例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./axis/wsdd/" xmlns:java="http://xml./axis/wsdd/providers/java"> <service name="MyService" provider="java:RPC"> <parameter name="className" value="MyService"/> <parameter name="allowedMethods" value="processService"/> </service> </deployment> 3. 啟動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. 重新啟動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)建一個服務(wù)(service)調(diào)用(call) Service service = new Service(); Call call = (Call) service.createCall();// 通過service創(chuàng)建call對象 // 設(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自動生成),且無法進行一些復(fù)雜的配置, Dynamic Invocation Interface(DII) 和 Stubs 方式的區(qū)別主要有兩個地方: ① 兩種不同的 endpoint , DII :http://localhost:8081/axis_example/jws/MyService.jws Stubs :http://localhost:8081/axis_example/services/MyService ② 兩種不同的編譯方式 DII :根據(jù)endpoint訪問web service時,axis自動編譯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ǔ)上采用了代理機制,實際上和DII區(qū)別不大,。 7. 編譯Client.java,運行其中的main方法進行測試,可以看到屏幕打印出:" This is Dynamic Proxy test!" (通過http://localhost:8081/axis_example/services/MyService?wsdl可以查看生成的WSDL文件——SOAP服務(wù)描述文件) 附 : axis 提供了wsdl2java工具,web service服務(wù)器端提供了一個地址,可以訪問到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會根據(jù)命令參數(shù) WSDL-URI 生成相應(yīng)的包名,如localhost\axis_example\jws\MyService_jws 上述命令會在 E:\project\axis_example\src\test\mytest 目錄下生成四個文件: MyServiceSoapBindingStub.java (相當(dāng)于上面的MyService.java) MyService_PortType.java (相當(dāng)于上面的MyServiceInterface.java) MyServiceService.java/MyServiceServiceLocator.java (Service Locator模式,隱藏了具體的業(yè)務(wù)邏輯) 編寫junit單元測試,在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. 編譯上面四個service文件,并編譯運行 TestClient.java ,看到屏幕打印出:" This is Junit Test!" |
|
|