J2EE社區(qū)

          茍有恒,何必三更起五更眠;
          最無益,只怕一日曝十日寒.
          posts - 241, comments - 318, trackbacks - 0, articles - 16

          java Web Service axis 基礎(chǔ)

          Posted on 2010-08-16 14:58 xcp 閱讀(1069) 評論(0)  編輯  收藏

          實(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)如下:

          axis.JPG
          目錄說明如下:

          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方式

          通過下面三個(gè)例子進(jìn)行說明。

          在開始例子前,把

          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.  無需編譯 編譯由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();// 通過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,啟動(dòng)tomcat

          5.  編譯Client.java,運(yùn)行其中的main方法進(jìn)行測試,可以看到屏幕打印出:"This is Test!",可以看到axis_example/WEB-INF目錄下生jwsClasses/jws/MyService.class文件——axis會(huì)根據(jù)你訪問時(shí)的endpoint,自動(dòng)編譯其中的*.jws文件,并置于生成的jwsClasses相應(yīng)目錄下。

          (通過http://localhost:8081/axis_example/jws/MyService.jws?wsdl可以查看生成的WSDL文件——SOAP服務(wù)描述文件)

          注1: 在上面的 new Object[]{"This is Test!"} 語句中,只傳遞了一個(gè)參數(shù)。如果MyServic.java中

          processService(String arg) 改寫為

          processService(String arg,String arg2)

          你可以通過new Object[]{"test","test2"}傳遞多個(gè)參數(shù)。

          注2: 啟動(dòng)tomcat 后控制臺出現(xiàn)下面警告:

          - Unable to find required classes (javax.activation.DataHandler and javax.mail.i

          nternet.MimeMultipart). Attachment support is disabled.

          這是因?yàn)槿鄙賏ctivation.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.  無需編譯,拷貝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,啟動(dòng)tomcat

          6.  編譯Client.java,運(yùn)行其中的main方法進(jìn)行測試,可以看到屏幕打印出:" 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();// 通過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自動(dòng)生成),且無法進(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訪問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)行測試,可以看到屏幕打印出:" This is Dynamic Proxy test!"

          (通過http://localhost:8081/axis_example/services/MyService?wsdl可以查看生成的WSDL文件——SOAP服務(wù)描述文件)

          axis 提供了wsdl2java工具,web service服務(wù)器端提供了一個(gè)地址,可以訪問到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單元測試,在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!"




          名稱: ?4C.ESL | .↗Evon
          口號: 遇到新問題?先要尋找一個(gè)方案乄而不是創(chuàng)造一個(gè)方案こ
          mail: 聯(lián)系我



          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 瑞安市| 南宫市| 天峻县| 中宁县| 扬中市| 江阴市| 新兴县| 桂平市| 江山市| 张家港市| 宣威市| 丹东市| 青阳县| 仙游县| 习水县| 平武县| 顺义区| 石河子市| 长寿区| 改则县| 买车| 葵青区| 蒙自县| 灵石县| 壶关县| 江城| 民丰县| 台江县| 遵义市| 桑植县| 聂荣县| 象山县| 新津县| 松阳县| 白朗县| 祁连县| 荥阳市| 杭州市| 宁海县| 金门县| 洪湖市|