qq4615

导航

使用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. }  
 

调用结果:

posted on 2018-05-23 18:06  圣彼得  阅读(1417)  评论(0编辑  收藏  举报