eclipse开发webservice-axis2/spring+maven

来源:互联网 发布:个人博客推荐 知乎 编辑:程序博客网 时间:2024/06/14 09:02

前言

本文将讲述使用eclipse发布axis2服务的环境准备到发布过程。


1、基础准备

eclipse需要两个插件(eclipse_axis2-插件-直接解压到dropins)

http://download.csdn.net/detail/bestcxx/9721775


然后是axis2的war包和bin包,前者我们可以直接看到axis2服务启动的样子,并且把我们的webservice服务直接添加到这个axis2项目中,后面的bin包则直接为eclipse开发

axis2提供了环境支持。


axis2的war和bin下载(当然你也可以从apache的官网下载较新版本)

下载地址

http://download.csdn.net/detail/bestcxx/9721766


你本地需要有可以启动的服务器,我这里是tomcat


2、解压axis2-1.7.4-war.zip和axis2-1.7.4-bin.zip

得到axis2.war和axis2-1.7.4

对于axis2.war的处理

放到tomcat服务器webapps下启动

访问 http://localhost:8080/axis2/


点击Administration ,右面有个username和password,这是axis2提供的demo的管理员登陆页,默认是  admin/axis2

如果是生产环境一定记得修改,位置为axis2\WEB-INF\conf\axis2.xml


点击Services,就可以看到axis2自带的webservice服务了


对于axis2-1.7.4的处理

把axis2-1.7.4复制到某一个位置,比如你的D盘然后需要在3中设置了

3、在eclipse中配置axis2的开发环境


点击preferences

然后如上图设置axis2的bin路径,里面的一些jar包等会为eclipse提供支持


eclipse中axis2的插件

eclipse的两个axis2的插件解压到eclipse/dropins,然后重启eclipse

这样,你的eclipse中就可以看到这两个东西了,一个用于将java方法生成为服务端,一个则用于生成客户端



4、下面将写个简单的例子

这个例子包含两个部分,一是服务的发布,二是服务的访问

服务的发布包含简单方法的发布和复杂方法的发布

服务的访问包含axis2客户端访问和axis2的RPC访问


4.1 方法1-接口和接口的实现

接口类

[java] view plain copy
  1. package stu.demo.service;  
  2.   
  3. public interface Axis2TestImpl {  
  4.     public String WriteStr(String str);  
  5. }  

实现类,也是具体服务的发布类

[java] view plain copy
  1. package stu.demo.service;  
  2.   
  3. public class AxisTestService implements Axis2TestImpl {  
  4.       
  5.     public String WriteStr(String str){  
  6.         System.out.println("您输入的是:"+str);  
  7.         return str;  
  8.     }  
  9.   
  10. }  

4.2  使用axis2的插件AxisTestService输出为webservice服务

注意红色方框圈中的区域,new来自于eclipse左上角的File


选择服务所在项目的classes文件夹


一路点下去


继续点


还点


再点一下,得到


Service name就是webservice的名字,Class name是方法所在类的路径,然后按下Load,下面会显示Method name,点next


那个location是文件输出路径,你自定义的,下面File name是输出文件的名字(我这个文是后面写的,所以名字和下面的配图不一样)

然后在你设置的位置得到下面的文件(axis2test_service.aar)



4.3 把服务发布到axis2.war中

还记得tomcat中的那个项目吗

先停止tomcat服务器

把axis2test_service.aar复制到axis2这个项目的axis2\WEB-INF\services\下

(记得把axis2.war删除)

然后重启tomcat

这样你就已经把你刚写的服务发布到这个axis2中去了

访问http://localhost:8080/axis2/services/listServices



4.4 编写并发布另外两个服务

刚才发布的是一个以接口实现类形式展现的类,返回的是String类型,下面将发布不实现任何接口的类,以及返回为实体类型的方法

发布的方法不再赘述


不实现接口的类发布为webservice服务

[java] view plain copy
  1. package stu.demo.noimpl;  
  2.   
  3. public class Axis2NoImpl {  
  4.       
  5.     public String  WriteYourName(String name){  
  6.         System.out.println("你输入的名字为:"+name);  
  7.         return name;  
  8.     }  
  9.   
  10. }  

eclipse产生的对应的aar文件为 axis2noImpl.aar



返回类型为实体类的发布为webservice服务

需要实体类

[java] view plain copy
  1. package stu.demo.fz;  
  2.   
  3. public class ModelUser {  
  4.       
  5.     private String userName;  
  6.     private String userPwd;  
  7.     public String getUserName() {  
  8.         return userName;  
  9.     }  
  10.     public void setUserName(String userName) {  
  11.         this.userName = userName;  
  12.     }  
  13.     public String getUserPwd() {  
  14.         return userPwd;  
  15.     }  
  16.     public void setUserPwd(String userPwd) {  
  17.         this.userPwd = userPwd;  
  18.     }  
  19.       
  20.       
  21.   
  22. }  


然后是包含发布方法的类

[java] view plain copy
  1. package stu.demo.fz;  
  2.   
  3. public class Axis2Fz {  
  4.     public ModelUser WriteModelUser(ModelUser modelUser){  
  5.         System.out.println("你输入的用户名为:"+modelUser.getUserName());  
  6.         System.out.println("你输入的密码为:"+modelUser.getUserPwd());  
  7.         return modelUser;  
  8.     }  
  9. }  

eclipse产生的对应的aar文件为 axis2Fz.aar



4.5 axis2插件生成客户端访问webservice



这里涉及到要为哪一个webservice设置客户端

所以先获取webservice的wsdl地址

方法如下


如下



然后接着说eclipse的下一步


Next


Next


选择输出到向项目,然后直接选择一个项目的路径(到项目名字即可)

然后Finesh

得到客户端



当然,如果你为复杂的返回类型那个生成客户端,则会有一个实体类文件生成




4.5 axis2的客户端调用和RPC调用

这里不赘述了了,直接把客户端的调用方法和PRC的调用方法贴出来

需要注意的是,如果只是PRC调用的话,对于返回参数为某个实体类的话,需手动创建实体类文件

需要强调的是,对于客户端调用,其方法名字是和你调用的服务的类的名字息息相关的,但是大体思路是

实例化Stub  stub

实例化入参类型 入参

实例化返回类型 返回

返回=stub.方法(入参)

返回.getreturn()就是返回值


[java] view plain copy
  1. package stu.demo.test;  
  2.   
  3. import java.rmi.RemoteException;  
  4.   
  5. import javax.xml.namespace.QName;  
  6.   
  7. import junit.framework.TestCase;  
  8.   
  9. import org.apache.axis2.AxisFault;  
  10. import org.apache.axis2.addressing.EndpointReference;  
  11. import org.apache.axis2.client.Options;  
  12. import org.apache.axis2.rpc.client.RPCServiceClient;  
  13. import org.junit.Test;  
  14.   
  15. import stu.demo.fz.xsd.ModelUser;  
  16. import stu.demo.noimpl.Axis2NoImplStub;  
  17. import stu.demo.noimpl.WriteYourName;  
  18. import stu.demo.noimpl.WriteYourNameResponse;  
  19.   
  20. public class Axis2ClientNoImpl {  
  21.     /** 
  22.      * 使用axis2客户端调用 
  23.      */  
  24.     @Test  
  25.     public void testAxis2NoImpl(){  
  26.         try {  
  27.             String name="Jecket";  
  28. //          Axis2NoImplStub Axis2NoImpl =new  Axis2NoImplStub();//这个方法访问地址被封装到了客户端代码  
  29.             Axis2NoImplStub Axis2NoImpl =new  Axis2NoImplStub("http://localhost:8080/axis2/services/Axis2NoImpl.Axis2NoImplHttpSoap12Endpoint/");//如果生产地址和测试不一致,则需要指定,灵活运用  
  30.               
  31.             WriteYourName writeYourName=new WriteYourName ();  
  32.             writeYourName.setName(name);  
  33.               
  34.             WriteYourNameResponse writeYourNameResponse=new WriteYourNameResponse();  
  35.               
  36.             writeYourNameResponse=Axis2NoImpl.writeYourName(writeYourName);  
  37.               
  38.             TestCase.assertFalse("方法返回的名字和你输入的名字不一致", writeYourNameResponse.get_return()==name);  
  39.         } catch (AxisFault e) {  
  40.             // TODO Auto-generated catch block  
  41.             e.printStackTrace();  
  42.         } catch (RemoteException e) {  
  43.             // TODO Auto-generated catch block  
  44.             e.printStackTrace();  
  45.         }  
  46.     }  
  47.       
  48.     /** 
  49.      * 使用axis2的RPC调用 
  50.      */  
  51.     @Test  
  52.     public void testAxis2NoImpl2(){  
  53.           String result = "";  
  54.           try {  
  55.   
  56.            String serviceUrl = "http://localhost:8080/axis2/services/Axis2NoImpl?wsdl";  
  57.            //使用RPC方式调用WebService  
  58.            RPCServiceClient serviceClient = new RPCServiceClient();  
  59.            Options options = serviceClient.getOptions();  
  60.            //设置2秒超时  
  61.            options.setTimeOutInMilliSeconds(2000L);  
  62.            //指定调用WebService的URL  
  63.            EndpointReference targetEPR = new EndpointReference(serviceUrl);  
  64.            options.setTo(targetEPR);  
  65.              
  66.            //指定接口方法的参数值  
  67.            Object[] opAddEntryArgs = new Object[] {"Jecket"};  
  68.            //指定方法返回值的数据类型的Class对象  
  69.            Class[] classes = new Class[] { String.class };  
  70.            //指定调用的方法及WSDL文件的命名空间  QName("targetNamespace","method Name");  
  71.            QName qName = new QName("http://noimpl.demo.stu","WriteYourName");  
  72.            //调用getVersioin方法并输出该方法的返回值,  
  73.            //返回对象是一个Object的数组,拿数组的第一个值,转换强转即可  
  74.            result = serviceClient.invokeBlocking(qName,opAddEntryArgs, classes)[0].toString();  
  75.           } catch (Exception e) {  
  76.            e.printStackTrace();  
  77.            result = e.getMessage();  
  78.              
  79.             
  80.           }  
  81.           System.out.println("返回值result="+result);  
  82.     }  
  83.       
  84.     /** 
  85.      * 使用axis2的RPC调用 
  86.      * 入参和反参都是复杂类型 
  87.      */  
  88.     @Test  
  89.     public void testAxis2Fz(){  
  90.           String result = "";  
  91.           try {  
  92.   
  93.            String serviceUrl = "http://localhost:8080/axis2/services/Axis2Fz?wsdl";  
  94.            //使用RPC方式调用WebService  
  95.            RPCServiceClient serviceClient = new RPCServiceClient();  
  96.            Options options = serviceClient.getOptions();  
  97.            //设置2秒超时  
  98.            options.setTimeOutInMilliSeconds(2000L);  
  99.            //指定调用WebService的URL  
  100.            EndpointReference targetEPR = new EndpointReference(serviceUrl);  
  101.            options.setTo(targetEPR);  
  102.              
  103.            //指定接口方法的参数值  
  104.            ModelUser modelUser=new ModelUser();  
  105.            modelUser.setUserName("Jecket");  
  106.            modelUser.setUserPwd("123456");  
  107.            Object[] opAddEntryArgs = new Object[] {modelUser};  
  108.            //指定方法返回值的数据类型的Class对象  
  109.            Class[] classes = new Class[] { ModelUser.class };  
  110.            //指定调用的方法及WSDL文件的命名空间  QName("targetNamespace","method Name");  
  111.            QName qName = new QName("http://fz.demo.stu","WriteModelUser");  
  112.            //调用getVersioin方法并输出该方法的返回值,  
  113.            //返回对象是一个Object的数组,拿数组的第一个值,转换强转即可  
  114.            modelUser = (ModelUser) serviceClient.invokeBlocking(qName,opAddEntryArgs, classes)[0];  
  115.            
  116.            System.out.println("name:"+modelUser.getUserName());  
  117.            System.out.println("password:"+modelUser.getUserPwd());  
  118.           } catch (Exception e) {  
  119.            e.printStackTrace();  
  120.            result = e.getMessage();  
  121.              
  122.             
  123.           }  
  124.            
  125.     }  
  126.       
  127.       
  128.   
  129. }  



4.6 axis2的webservice服务添加到自己的项目中发布结合spring+maven

首先建议阅读官方文档http://axis.apache.org/axis2/java/core/docs/spring.html

然后就是实践了

1、maven的pom.xml中关于axis2的配置

[html] view plain copy
  1. <dependency>  
  2.             <groupId>org.apache.axis2</groupId>  
  3.             <artifactId>axis2-transport-http</artifactId>  
  4.             <version>1.7.2</version>  
  5.         </dependency>  
  6.         <dependency>  
  7.             <groupId>org.apache.axis2</groupId>  
  8.             <artifactId>axis2-spring</artifactId>  
  9.             <version>1.7.2</version>  
  10.         </dependency>  
  11.         <dependency>  
  12.             <groupId>org.apache.axis2</groupId>  
  13.             <artifactId>axis2</artifactId>  
  14.             <version>1.6.2</version>  
  15.         </dependency>  
  16.         <dependency>  
  17.             <groupId>org.apache.axis2</groupId>  
  18.             <artifactId>axis2-transport-local</artifactId>  
  19.             <version>1.7.2</version>  
  20.         </dependency>  
  21.         <dependency>  
  22.             <groupId>org.apache.axis2</groupId>  
  23.             <artifactId>axis2-kernel</artifactId>  
  24.             <version>1.6.2</version>  
  25.         </dependency>  

2、编写axis2的对外提供服务的类以及方法(我这里不写接口了,熟悉spring的应该知道最好使用接口以及接口的实现)

我的项目名称是 mavenssh,包路径为com.bestcxx.mavenstu.mavenssh.axis2,类名为Axis2Webservice,方法只有一个叫getStrA

[java] view plain copy
  1. package com.bestcxx.mavenstu.mavenssh.axis2;  
  2.   
  3. public class Axis2Webservice {  
  4.       
  5.     public String getStrA(){  
  6.         String str="123";  
  7.         System.out.println("你输入的是:"+str);  
  8.         return str;  
  9.     }  
  10.   
  11. }  


3、spring的applicationContext.xml中将Axis2Webservice注册为bean

[html] view plain copy
  1. <!--如果没有ServletContext配置 则需要增加下面这句 -->  
  2.     <!--<bean id="applicationContext"   
  3.     class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" /> -->  
  4.   
  5.     <bean id="springAwareService" class="com.bestcxx.mavenstu.mavenssh.axis2.Axis2Webservice" scope="prototype"/>  

4、web.xml编写

[html] view plain copy
  1. <listener>  
  2.         <description>Spring ApplicationContext 载入</description>  
  3.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  4.     </listener>  
  5. <!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔 此参数用于后面的Spring Context   
  6.         Loader -->  
  7.     <context-param>  
  8.         <param-name>contextConfigLocation</param-name>  
  9.         <param-value>classpath:spring/applicationContext.xml</param-value>  
  10.     </context-param>  
  11.   
  12. <!-- axis2设置 -->  
  13.      <servlet>    
  14.         <servlet-name>AxisServlet</servlet-name>      
  15.         <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>    
  16.     </servlet>   
  17.   
  18.     <servlet-mapping>  
  19.         <servlet-name>AxisServlet</servlet-name>  
  20.         <url-pattern>/services/*</url-pattern>  
  21.     </servlet-mapping>  

5、最后是services.xml的编写,这里需要注意路径

比如这里我的项目名称为mavenssh

就需要把services.xml放置到项目的如下路径中

-webapp

  -WEB-INF

    -services

      -mavenssh

        -META-INF

          -services.xml




services.xml的内容为

[html] view plain copy
  1. <service name="SpringAwareService"><!-- 访问的时候,这个是wsdl服务的名字 -->  
  2.     <description>  
  3.         simple spring example  
  4.     </description>  
  5.     <parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier</parameter>  
  6.     <parameter name="SpringBeanName">springAwareService</parameter><!-- 这个是spring中配置的bean名字 -->  
  7.     <parameter name="getStrA"><!-- 这个是对外提供的服务的具体方法名 -->  
  8.    <span style="white-space:pre">   </span> <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>  
  9.     </parameter>  
  10. </service>   


6、这样之后,服务就已经可以正常启动和访问了

http://localhost:8085/mavenssh/services/SpringAwareService?wsdl


7、但是控制台提示

 Please update your axis2.xml file!

只需把我们上面实验的axis2.war中的WEB_INF/conf/下的axis2.xml复制到mavenssh(你的项目)下的WEB_INF目录下即可

里面有个用户名和密码,建议注释掉。



转载请声明出处:http://blog.csdn.net/bestcxx/article/details/53889270


原创粉丝点击