J2EE社區

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

          java Web Service axis 基礎

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

          實例(參考了 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目錄結構如下:

          axis.JPG
          目錄說明如下:

          jws :存放*.jws文件

          src :java源碼

          WEB-INF/classes :java編譯后的class文件

          WEB-INF/lib :需要用到的jar包

          Axis 支持三種web service開發方式,分別為:

          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文件,內容為:

          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文件,內容為:

          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";

                  // 創建一個服務(service)調用(call)

                  Service service = new Service();

                  Call call = (Call) service.createCall();// 通過service創建call對象

                  // 設置service所在URL

                  call.setTargetEndpointAddress(new java.net.URL(endpoint));

                  // 方法名(processService)與MyService.java方法名保持一致

                  call.setOperationName("processService");

                  // Object 數組封裝了參數,參數為"This is Test!",調用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會根據你訪問時的endpoint,自動編譯其中的*.jws文件,并置于生成的jwsClasses相應目錄下。

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

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

          processService(String arg) 改寫為

          processService(String arg,String arg2)

          你可以通過new Object[]{"test","test2"}傳遞多個參數。

          注2: 啟動tomcat 后控制臺出現下面警告:

          - 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.sun.com/products/javabeans/jaf/downloads/index.html

          mail.jar (目前版本為1.4)下載地址

          http://java.sun.com/products/javamail/downloads/

          實例2(Dynamic Proxy)步驟

          1.  axis_example /src 新建一MyServiceInterface.java文件,內容為:

          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方法,內容為:

              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文件,內容為:

          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)文件,內容為:

          <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.  啟動tomcat

          4.  axis_example/WEB-INF目錄下執行:

          java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://localhost:8081/axis_example/servlet/AxisServlet deploy.wsdd

          執行后可看到在axis_example/WEB-INF目錄下生成server-config.wsdd文件。

          5.  重新啟動tomcat ,以便加載 server-config.wsdd 文件。

          6.  更改axis_example/src/Client.java中的main方法,內容為:

              public static void main(String [] args) throws Exception {

                  // 指出service所在URL

                  String endpoint = "http://localhost:" + "8081" + "/axis_example/services/MyService";

                  // 創建一個服務(service)調用(call)

                  Service service = new Service();

                  Call call = (Call) service.createCall();// 通過service創建call對象

                  // 設置service所在URL

                  call.setTargetEndpointAddress(new java.net.URL(endpoint));

                  // 方法名(processService)與MyService.java方法名保持一致

                  call.setOperationName("processService");

                  // Object 數組封裝了參數,參數為"This is Test!",調用processService(String arg)

                  String ret = (String) call.invoke(new Object[]{"This is Test!"});

                  System.out.println(ret);

              }

          注: 在這里可以看出, DII 方式安全性不高(url MyService.jws為axis自動生成),且無法進行一些復雜的配置, Dynamic Invocation Interface(DII) Stubs 方式的區別主要有兩個地方:

          ① 兩種不同的 endpoint

          DII :http://localhost:8081/axis_example/jws/MyService.jws

          Stubs http://localhost:8081/axis_example/services/MyService

          ② 兩種不同的編譯方式

          DII :根據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的基礎上采用了代理機制,實際上和DII區別不大,。

          7.  編譯Client.java,運行其中的main方法進行測試,可以看到屏幕打印出:" This is Dynamic Proxy test!"

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

          axis 提供了wsdl2java工具,web service服務器端提供了一個地址,可以訪問到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

          生成相應的客戶端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

          生成相應的客戶端java文件。

          參數

          -p  指定生成的java文件包名

          -o  指定生成的java文件輸出目錄

          如果不指定包名,axis會根據命令參數 WSDL-URI 生成相應的包名,如localhost"axis_example"jws"MyService_jws

          上述命令會在 E:"project"axis_example"src"test"mytest 目錄下生成四個文件:

          MyServiceSoapBindingStub.java (相當于上面的MyService.java)

          MyService_PortType.java (相當于上面的MyServiceInterface.java)

          MyServiceService.java/MyServiceServiceLocator.java (Service Locator模式,隱藏了具體的業務邏輯)

          編寫junit單元測試,在axis_example"src"test"mytest下新建一TestClient.java文件(拷貝junit.jar包到axis_example/WEB-INF目錄下),內容為:

          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!"




          名稱: ?4C.ESL | .↗Evon
          口號: 遇到新問題?先要尋找一個方案乄而不是創造一個方案こ
          mail: 聯系我



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


          網站導航:
           
          主站蜘蛛池模板: 北川| 海伦市| 镇康县| 赤水市| 通道| 肥西县| 平安县| 鲁山县| 三台县| 固原市| 彰武县| 瓦房店市| 安乡县| 道孚县| 内江市| 光山县| 沁阳市| 乡城县| 日照市| 交城县| 韩城市| 江孜县| 洪江市| 阜新市| 松潘县| 西城区| 平武县| 阿克陶县| 洛南县| 金湖县| 江永县| 资源县| 河北区| 慈溪市| 鄢陵县| 三门峡市| 罗江县| 秦安县| 花莲县| 开鲁县| 北票市|