使用Axis2實現WebService的發布和調用


轉載 https://blog.csdn.net/kris234seth/article/details/50456944

使用Axis2實現WebService的發布和調用

 

一、Axis2簡介:

Axis2是一套嶄新的WebService引擎,該版本是對Axis1.x重新設計的產物。Axis2不僅支持SOAP1.1和SOAP1.2,還集成了非常流行的REST WebService,同時還支持Spring、JSON等技術。在本文中主要介紹了如何使用Axis2開發一個不需要任何配置文件的WebService,並在客戶端使用Java調用這個WebService。

二、Axis2下載安裝:

 Axis下載地址:  http://ws.apache.org/axis2/

 在本文使用了目前Axis2的最新版本1.6.4。讀者可以下載如下兩個zip包:   

   axis2-1..6.4-bin.zip     

   axis2-1.6.4-war.zip 
   

其中 axis2-1.6.4-bin.zip 文件中包含了Axis2中所有的jar文件,axis2-1.6.4-war.zip 文件用於將WebService發布到Web容器中。

將 axis2-1.6.4-war.zip 文件解壓到相應的目錄,將目錄中的axis2.war文件放到Tomcat服務器的webapps目錄中(本文使用的Tomcat的版本是7.x),並啟動Tomcat。 

在瀏覽器地址欄中輸入如下的URL:     http://localhost:8080/axis2/ 

如果在瀏覽器中顯示出如下所示的頁面,則表示Axis2安裝成功。

三、編寫和發布WebService

在Axis2中不需要進行任何的配置,就可以直接將一個簡單的POJO發布成WebService。其中POJO中所有的public方法將被發布成WebService方法。

新建一個Java工程Axis2_1,直接點擊src右鍵新建一個名為 SimpleService 的類,這樣建好的Java文件會默認放在 default package中,在Java代碼中將不會出現package 定義語句 (這很重要,因為發布webservice服務的class文件不能使用package關鍵字聲明包)

SimpleService類:

 

[java]  view plain  copy
 
  1. import java.util.Random;  
  2. //注意:無package包聲明  
  3. public class SimpleService {  
  4.       
  5.     public  String getGreeting(String name){  
  6.         return "Hello " + name;  
  7.     }  
  8.       
  9.     public int getPrice(){  
  10.         return new Random().nextInt(100);  
  11.     }  
  12. }  

在SimpleService類中有兩個方法,由於這兩個方法都是public方法,因此,它們都將作為WebService方法被發布

 

 編譯SimpleService類后,將SimpleService.class文件放到Tomcat容器的webapps\axis2\WEB-INF\pojo目錄中(如果沒有pojo目錄,則新建該目錄)。現在我們已經成功將SimpleService類發布成了WebService。在瀏覽器地址欄中輸入如下的URL: 
http://localhost:8080/axis2/services/listServices ,如果成功發布,則出現下圖:


注意事項:

1. POJO類不能使用package關鍵字聲明包。 
2. Axis2在默認情況下可以熱發布WebService,也就是說,將WebService的.class文件復制到pojo目錄中時,Tomcat不需要重新啟動就可以自動發布WebService。如果想取消Axis2的熱發布功能,可以打開\webapps\axis2\WEB-INF\conf\axis2.xml,找到如下的配置代碼:

 

[html]  view plain  copy
 
  1. <parameter name="hotdeployment">true</parameter>  

將true改為false即可。要注意的是,Axis2在默認情況下雖然是熱發布,但並不是熱更新,也就是說,一旦成功發布了WebService,再想更新該WebService,就必須重啟Tomcat。這對於開發人員調試WebService非常不方便,因此,在開發WebService時,可以將Axis2設為熱更新,同樣在axis2.xml文件中找到如下配置:

 

 

[html]  view plain  copy
 
  1. <parameter name="hotupdate">false</parameter>  

將false改為true 即可

 

 3. 發布WebService的pojo目錄只是默認的,如果想在其他的目錄發布WebService,可以打開axis2.xml文件,並在<axisconfig>元素中添加如下的子元素:

 

[html]  view plain  copy
 
  1. <deployer extension=".class" directory="pojo" class="org.apache.axis2.deployment.POJODeployer"/>  

directory屬性的值 設置為你的發布目錄即可

 

四、Java調用WebService

導入用到的jar包:

 

[plain]  view plain  copy
 
  1. axiom-api-1.2.15.jar  
  2. axiom-impl-1.2.15.jar  
  3. axis2-adb-1.6.4.jar  
  4. axis2-kernel-1.6.4.jar  
  5. axis2-transport-http-1.6.4.jar  
  6. axis2-transport-local-1.6.4.jar  
  7. commons-codec-1.3.jar  
  8. commons-httpclient-3.1.jar  
  9. commons-logging-1.1.1.jar  
  10. httpcore-4.0.jar  
  11. neethi-3.0.2.jar  
  12. wsdl4j-1.6.2.jar  
  13. XmlSchema-1.4.7.jar  

 

Java調用代碼(RPC方式):

 

[java]  view plain  copy
 
  1. public class RPCClient {  
  2.       
  3.     public static String address="http://localhost:8080/axis2/services/SimpleService";  
  4.       
  5.     public static void main(String[] args) throws IOException{  
  6.           
  7.         Object[] result=invoke("getPrice", new Object[]{}, new Class[]{int.class});  
  8.         System.out.println(result[0]);  
  9.         result=invoke("getGreeting", new Object[]{"jack"}, new Class[]{String.class});  
  10.         System.out.println(result[0]);  
  11.     }  
  12.       
  13.         @SuppressWarnings("rawtypes")  
  14.     public static Object[] invoke(String method,Object[] params,Class[] classes) throws AxisFault{  
  15.         //使用RPC方式調用WebService  
  16.         RPCServiceClient client=new RPCServiceClient();  
  17.         Options option=client.getOptions();  
  18.           
  19.         //指定調用的URL  
  20.         EndpointReference reference=new EndpointReference(address);  
  21.         option.setTo(reference);  
  22.           
  23.         /* 
  24.          * 設置要調用的方法 
  25.          * http://ws.apache.org/axis2 為默認的(無package的情況)命名空間, 
  26.          * 如果有包名,則為 http://axis2.webservice.elgin.com ,包名倒過來即可 
  27.          * method為方法名稱 
  28.          *  
  29.          */  
  30.         QName  qname=new QName("http://ws.apache.org/axis2", method);  
  31.           
  32.         //調用遠程方法,並指定方法參數以及返回值類型  
  33.         Object[] result=client.invokeBlocking(qname,params,classes);  
  34.           
  35.         return result;  
  36.           
  37.     }  

輸出結果:

 

五、Java復雜數據的調用處理

在default package下新建返回復雜類型的WebService類

 

[java]  view plain  copy
 
  1. import java.io.File;  
  2. import java.io.FileOutputStream;  
  3. import java.io.IOException;  
  4. import java.util.Random;  
  5. import com.elgin.webservice.axis2.User;  
  6.   
  7. public class ComplexTypeServices {  
  8.       
  9.     /** 
  10.     * @Title: upload 
  11.     * @Description: 文件數據處理 
  12.     * @param b 
  13.     * @param len 
  14.     * @return    參數 
  15.     */  
  16.     public String upload(byte[] b ,int len){  
  17.         FileOutputStream fos=null;  
  18.         String path="";  
  19.         try {  
  20.             String dir=System.getProperty("user.dir");  
  21.             File file=new File(dir + "/" + new Random().nextInt(1000) + ".jsp");  
  22.             fos=new FileOutputStream(file);  
  23.             fos.write(b , 0 ,len);  
  24.             path=file.getAbsolutePath();  
  25.             System.out.println("File path:" + path);  
  26.         } catch (Exception e) {  
  27.               
  28.         } finally{  
  29.             try {  
  30.                 fos.close();  
  31.             } catch (IOException e) {  
  32.                 e.printStackTrace();  
  33.             }  
  34.         }  
  35.         return path;  
  36.     }  
  37.       
  38.     /** 
  39.     * @Title: getArray 
  40.     * @Description: 返回一維數組 
  41.     * @param i      數組長度 
  42.     * @return       參數 
  43.     */  
  44.     public int[] getArray(int i){  
  45.         int[] arr=new int[i];  
  46.         for (int j = 0; j < arr.length; j++) {  
  47.             arr[j]=new Random().nextInt(100);  
  48.         }  
  49.         return arr;  
  50.     }  
  51.       
  52.       
  53.     /** 
  54.     * @Title: getTwoArray 
  55.     * @Description: 返回二維數組 
  56.     * @return    參數 
  57.     */  
  58.     public String[][] getTwoArray(){  
  59.         return new String[][]{{"北京","上海"},{"南京","蘇州"},{"深圳","廈門"},{"西安","蘭州"}};  
  60.     }  
  61.       
  62.       
  63.     /** 
  64.     * @Title: getUser 
  65.     * @Description: 返回JavaBean對象 
  66.     * @return    參數 
  67.     */  
  68.     public User getUser(){  
  69.         User user=new User();  
  70.         user.setUsername("elgin");  
  71.         user.setAge(26);  
  72.         user.setEmail("3303335@qq.com");  
  73.         return user;  
  74.     }  
  75. }  

User類:

 

 

[java]  view plain  copy
 
  1. package com.elgin.webservice.axis2;  
  2.   
  3. public class User implements Serializable{  
  4.         private static final long serialVersionUID = 1L;  
  5.     private String username;  
  6.     private int age;  
  7.     private String email;  
  8.       
  9.     public String getUsername() {  
  10.         return username;  
  11.     }  
  12.     public void setUsername(String username) {  
  13.         this.username = username;  
  14.     }  
  15.     public int getAge() {  
  16.         return age;  
  17.     }  
  18.     public void setAge(int age) {  
  19.         this.age = age;  
  20.     }  
  21.     public String getEmail() {  
  22.         return email;  
  23.     }  
  24.     public void setEmail(String email) {  
  25.         this.email = email;  
  26.     }  
  27.     @Override  
  28.     public String toString() {  
  29.         return "User [username=" + username + ", age=" + age + ", email=" + email + "]";  
  30.     }  
  31. }  

編譯完成之后,將上述ComplexTypeServices類的class文件放到Tomcat容器的webapps\axis2\WEB-INF\pojo目錄下,在Tomcat的webapps/axis2工程下的classes文件中新建文件路徑:com/elgin/webservice/axis2  (User的包路徑) ,然后將User的class文件放到此文件夾下,重啟Tomcat,訪問

 

http://localhost:8080/axis2/services/listServices   可以看到發布成功了,在list中有 ComplexTypeServices這個服務

逐一調用各個方法:

 

[java]  view plain  copy
 
  1. package com.elgin.webservice.axis2;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.IOException;  
  6. import javax.xml.namespace.QName;  
  7.   
  8. import org.apache.axis2.AxisFault;  
  9. import org.apache.axis2.addressing.EndpointReference;  
  10. import org.apache.axis2.client.Options;  
  11. import org.apache.axis2.rpc.client.RPCServiceClient;  
  12.   
  13.   
  14. public class RPCClient {  
  15.       
  16.     public static String address="http://localhost:8080/axis2/services/SimpleService";  
  17.     public static String address1="http://localhost:8080/axis2/services/ComplexTypeServices";  
  18.       
  19.     public static void main(String[] args) throws IOException{  
  20.           
  21.         testUpload();  
  22.         testArray();  
  23.         testTwoArray();  
  24.         testUser();  
  25.           
  26.     }  
  27.       
  28.     @SuppressWarnings("rawtypes")  
  29.     public static Object[] invoke(String method,Object[] params,Class[] classes) throws AxisFault{  
  30.         //使用RPC方式調用WebService  
  31.         RPCServiceClient client=new RPCServiceClient();  
  32.         Options option=client.getOptions();  
  33.           
  34.         //指定調用的URL  
  35.         EndpointReference reference=new EndpointReference(address1);  
  36.         option.setTo(reference);  
  37.           
  38.         /* 
  39.          * 設置要調用的方法 
  40.          * http://ws.apache.org/axis2 為默認的(無package的情況)命名空間, 
  41.          * 如果有包名,則為 http://axis2.webservice.elgin.com ,包名倒過來即可 
  42.          * method為方法名稱 
  43.          *  
  44.          */  
  45.         QName  qname=new QName("http://ws.apache.org/axis2", method);  
  46.           
  47.         //調用遠程方法,並指定方法參數以及返回值類型  
  48.         Object[] result=client.invokeBlocking(qname,params,classes);  
  49.           
  50.         return result;  
  51.           
  52.     }  
  53.       
  54.     public static void testUpload() throws IOException{  
  55.         String dir=System.getProperty("user.dir");  
  56.         File file=new File(dir +"/WebContent"+ "/hello.jsp");  
  57.         FileInputStream fis=new FileInputStream(file);  
  58.         int len=(int) file.length();  
  59.         byte[] b=new byte[len];  
  60.         int read=fis.read(b);  
  61.         fis.close();  
  62.         Object[] result=invoke("upload", new Object[]{b,read}, new Class[]{String.class});  
  63.         System.out.println(result[0]);  
  64.     }  
  65.       
  66.     public static void testArray() throws AxisFault{  
  67.         Object[] result=invoke("getArray", new Object[]{5}, new Class[]{int[].class});  
  68.         int[] arr=(int[]) result[0];  
  69.         for (int i : arr) {  
  70.             System.out.println(i );  
  71.         }  
  72.     }  
  73.       
  74.     public static void testTwoArray() throws AxisFault{  
  75.         Object[] result=invoke("getTwoArray", new Object[]{}, new Class[]{String[][].class});  
  76.         String[][] arr=(String[][]) result[0];  
  77.         for (String[] strings : arr) {  
  78.             for (String str : strings) {  
  79.                 System.out.println(str);  
  80.             }  
  81.         }  
  82.     }  
  83.       
  84.     public static void testUser() throws AxisFault{  
  85.         Object[] result=invoke("getUser", new Object[]{}, new Class[]{User.class});  
  86.         User user=(User) result[0];  
  87.         System.out.println(user.toString());  
  88.     }  
  89.       
  90. }  
 

調用結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM