Axis2与web项目整合、与spring整合、在maven下与spring整合

原文地址:https://blog.csdn.net/qq877507054/article/details/62039639

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

   在本文使用了目前Axis2的最新版本1.4.1。读者可以下载如下两个zip包:

   axis2-1.4.1-bin.zip

   axis2-1.4.1-war.zip

   其中axis2-1.4.1-bin.zip文件中包含了Axis2中所有的jar文件, axis2-1.4.1-war.zip文件用于将WebService发布到Web容器中。

   axis2-1.4.1-war.zip文件解压到相应的目录,将目录中的axis2.war文件放到<Tomcat安装目录>\webapps目录中(本文使用的Tomcat的版本是6.x),并启动Tomcat

   在浏览器地址栏中输入如下的URL

   http://localhost:8080/axis2/

   如果在浏览器中显示出如图1所示的页面,则表示Axis2安装成功。


一:与web项目的整合

①   新建一个动态的web工程 (Dynamic Web project)

      本例中新建的工程名为:AxisWebDemo

②  将上次下载的  axis2-1.6.4-war.zip 文件解压到相应的目录,将目录中的axis2.war文件放到Tomcat服务器的webapps目录中(本文使用的Tomcat的版本是7.x),并启动Tomcat。 这时在Tomcat的webapps文件夹中会出现一个 名为 axis2 的文件夹(其实这就是部署好的 axis2工程),如下图


③   将 axis2 下的 axis2-web 文件夹移动到我们 AxisWebDemo 工程的 WebContent目录下 

       将WEB-INF 下的 lib 、conf  、modules文件夹移动到 AxisWebDemo工程的 WEB-INF目录下 ,结构如下图:


④   在src右键,新建package : com.elgin.webservice   

在这个包中新建一个我们需要发布的 WebService 服务的类: WebServiceDemo

      代码如下:

[java]  view plain  copy
  1. package com.elgin.webservice;    
  2.     
  3. public class WebServiceDemo {    
  4.        
  5.     public String  sayHello(String name){    
  6.         return "hello " + name;    
  7.     }    
  8.         
  9.     public int getAge(){    
  10.         return 26;    
  11.     }    
  12. }    

⑤   在 AxisWebDemo 工程的 WEB-INF 下新建如下层次结构目录 : services/webservices/META-INF/services.xml  ,如下图所示:


经过我的验证,发现:目录层次必须是  services / 任意名称文件夹  / META-INF / services.xml   ,否则 WebService发布会失败 ,也就是说上图的层次中 ,只有webservice这个文件夹的名字是可以自由指定,其它的文件、文件夹名字都是固定的!

services.xml 配置文件内容:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <serviceGroup>      
  3.        <!- name属性配置WebService的名称 ->    
  4.        <service name="myService">      
  5.               <description>Web Service</description>      
  6.               <!-- ServiceClass属性配置提供WebService服务类的全类名 -->    
  7.               <parameter name="ServiceClass">com.elgin.webservice.WebServiceDemo</parameter>      
  8.               <messageReceivers>      
  9.                      <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />      
  10.                      <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />      
  11.               </messageReceivers>      
  12.        </service>      
  13. </serviceGroup>     

⑥   在  AxisWebDemo 工程的 web.xml 文件中加入axis2的配置支持:

[html]  view plain  copy
  1. <!-- 加入axis2支持 -->    
  2.   <servlet>    
  3.         <servlet-name>AxisServlet</servlet-name>    
  4.         <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>    
  5.         <load-on-startup>1</load-on-startup>    
  6.   </servlet>    
  7.   <servlet-mapping>    
  8.         <servlet-name>AxisServlet</servlet-name>    
  9.         <url-pattern>/services/*</url-pattern>    
  10.   </servlet-mapping>    

经过以上6个步骤 , axis2 已经整合到工程中了 ,把项目加入的Tomcat中,启动Tomcat服务器 ,在浏览器中输入以下URL:

http://localhost:8080/AxisWebDemo/services/listServices  

如果出现下图所示的信息,说明你的 WebService服务发布成功了


上图中的 myService 即是你在 services.xml 中配置的 WebService的名称

输入下面URL:

http://localhost:8080/AxisWebDemo/services/myService?wsdl

出现下图所显示的关于服务的xml信息:



注意上图中  <wsdl:definitions> 元素中的 targetNamespace的值  ,上一篇介绍中也提到过 ,是提供WebService服务的类所在的包名倒过来。

三、调用上面工程中发布的WebService服务:

Java调用代码:

[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 address2="http://localhost:8080/AxisWebDemo/services/myService?wsdl";    
  17.         
  18.     public static void main(String[] args) throws IOException{    
  19.         
  20.         testWebDemo();    
  21.             
  22.     }    
  23.         
  24.     @SuppressWarnings("rawtypes")    
  25.     public static Object[] invoke(String method,Object[] params,Class[] classes) throws AxisFault{    
  26.         //使用RPC方式调用WebService    
  27.         RPCServiceClient client=new RPCServiceClient();    
  28.         Options option=client.getOptions();    
  29.             
  30.         //指定调用的URL    
  31.         EndpointReference reference=new EndpointReference(address2);    
  32.         option.setTo(reference);    
  33.             
  34.         /*  
  35.          * 设置要调用的方法  
  36.          * http://ws.apache.org/axis2 为默认的(无package的情况)命名空间,  
  37.          * 如果有包名,则为 http://axis2.webservice.elgin.com ,包名倒过来即可  
  38.          * method为方法名称  
  39.          *   
  40.          */    
  41.         QName  qname=new QName("http://webservice.elgin.com", method);    
  42.             
  43.         //调用远程方法,并指定方法参数以及返回值类型    
  44.         Object[] result=client.invokeBlocking(qname,params,classes);    
  45.             
  46.         return result;    
  47.             
  48.     }    
  49.         
  50.     public static void testWebDemo() throws AxisFault{    
  51.         Object[] result=invoke("sayHello"new Object[]{"elgin"}, new Class[]{String.class});    
  52.         System.out.println(result[0]);    
  53.         result=invoke("getAge"new Object[]{}, new Class[]{int.class});    
  54.         System.out.println(result[0]);    
  55.     }    
  56.         
  57. }    

运行结果:


原文地址:Axis2与Web项目整合

二:Axis2与spring整合

①   新建项目 AxisSpringDemo,并在其中加入 Axis2 与 Spring 相关的 jar 包

Spring所需 Jar :

[plain]  view plain  copy
  1. aopalliance-1.0.jar    
  2. aspectjrt.jar    
  3. aspectjweaver.jar    
  4. spring-aop-3.2.1.RELEASE.jar    
  5. spring-beans-3.2.1.RELEASE.jar    
  6. spring-context-3.2.1.RELEASE.jar    
  7. spring-core-3.2.1.RELEASE.jar    
  8. spring-expression-3.2.1.RELEASE.jar    
  9. spring-tx-3.2.1.RELEASE.jar    
  10. spring-web-3.2.1.RELEASE.jar    
Axis2 所需 Jar :

[plain]  view plain  copy
  1. activation-1.1.jar    
  2. axiom-api-1.2.15.jar    
  3. axiom-impl-1.2.15.jar    
  4. axis2-adb-1.6.4.jar    
  5. axis2-jaxws-1.6.4.jar    
  6. axis2-kernel-1.6.4.jar    
  7. axis2-spring-1.6.4.jar    
  8. axis2-transport-http-1.6.4.jar    
  9. axis2-transport-local-1.6.4.jar    
  10. axis2-xmlbeans-1.6.4.jar    
  11. commons-fileupload-1.3.1.jar    
  12. commons-httpclient-3.1.jar    
  13. commons-io-2.1.jar    
  14. commons-logging-1.1.1.jar    
  15. geronimo-stax-api_1.0_spec-1.0.1.jar    
  16. httpcore-4.0.jar    
  17. jsr311-api-1.1.1.jar    
  18. mail-1.4.jar    
  19. neethi-3.0.2.jar    
  20. woden-api-1.0M9.jar    
  21. wsdl4j-1.6.2.jar    
  22. xml-resolver-1.2.jar    
  23. XmlSchema-1.4.7.jar    
②  在工程中的 web.xml 文件中加入 Spring 、 Axis2支持配置:

[html]  view plain  copy
  1. <!-- 加入Spring支持 -->    
  2.  <context-param>    
  3.    <param-name>contextConfigLocation</param-name>    
  4.    <param-value>classpath:applicationContext.xml</param-value>    
  5.  </context-param>    
  6.  <listener>    
  7.    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
  8.  </listener>    
  9.      
  10.  <!--加入Axis2支持  -->    
  11.  <servlet>    
  12.        <servlet-name>AxisServlet</servlet-name>    
  13.        <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>    
  14.        <load-on-startup>1</load-on-startup>    
  15.  </servlet>    
  16.  <servlet-mapping>    
  17.        <servlet-name>AxisServlet</servlet-name>    
  18.        <url-pattern>/services/*</url-pattern>    
  19.  </servlet-mapping>    
③  同上篇讲的整合 web项目一致 ,将conf 、axis2-web  、modules文件夹移动到 AxisSpringDemo工程的 下各个对应的位置。如图:


④  在src下新建包  com.elgin.spring.webservice ,并新建提供WebService服务的类  SpringWebServiceDemo ,代码如下:

[java]  view plain  copy
  1. package com.elgin.spring.webservice;    
  2.     
  3. import java.util.Random;    
  4.     
  5. import org.springframework.stereotype.Component;    
  6.     
  7. @Component("springWebService")    
  8. public class SpringWebServiceDemo {    
  9.         
  10.     public String springHello(){    
  11.         return "hello spring-axis2";     
  12.     }    
  13.         
  14.     public int getAge(){    
  15.         return new Random().nextInt(80);    
  16.     }    
  17.         
  18.     public void update(){    
  19.         System.out.println("update something..");    
  20.     }    
  21. }   

⑤  在类路径下新建 Spring配置文件 :applicationContxt.xml 配置文件

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"    
  4.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"    
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd    
  6.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd    
  7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd    
  8.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">    
  9.     
  10.     <!-- 配置spring注解扫描的包 -->    
  11.     <context:component-scan base-package="com.elgin"></context:component-scan>    
  12. <!-- 或者不用注解的形式加载bean,改用配置的方式    
  13. <bean id="springWebService" class="com.elgin.spring.webservice.SpringWebServiceDemo ">      
  14. 利用property可以对SpringService类进行初始化,比如<property name="name" value="姚明" /><property name="job" value="职业男篮" />,在配置完SpringService类后,就可以直接在程序中FileSystemXmlApplicationContext类或其他类似功能的类读取applicationContext.xml文件中的内容,并获得SpringService类的对象实例。但现在我们并不这样做,而是将SpringService类发布成WebService。在Tomcat的webapps项目中的WEB-INF\lib目录中有一个axis2-spring-1.4.1.jar文件, 该文件用于将被装配JavaBean的发布成WebService。  
  15.   -->    
  16. </beans>    


⑥  配置 Axis2的WebService服务:

同上一篇所说:在 AxisWebDemo 工程的 WEB-INF 下新建如下层次结构目录 : services/springServices/META-INF/services.xml 

services.xml配置内容:


[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>      
  2. <serviceGroup>        
  3.    <service name="springService">        
  4.       <description>Web Service</description>        
  5.    <!--     
  6.  SpringBeanName作用类似于普通配置中的ServiceClass,都是用来创建服务类对象,只不过普通配置使用反射来创建   
  7. 加入Spring之后,对象的创建交给了Spring的IOC容器,SpringBeanName指定要发布成WebService的Java类在applicationContext.xml文件中装配,  
  8. SpringBeanName参数是JavaBean的名称。SpringBeanName固定的不能改 ,因为springWebService是spring中注册的实现类得id   
  9. 如果不使用spring,可以使用ServiceClass属性,ServiceClass参数要指定要发布成WebService的Java类,并指定全类名的方式:com.elgin.spring.webservice.SpringWebServiceDemo  -->          
  10.       <parameter name="SpringBeanName">springWebService</parameter>      
  11. <!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象 -->    
  12.       <parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier</parameter>        
  13.        <!--       
  14.            在这里最值得注意的是<messageReceivers>元素,该元素用于设置处理WebService方法的处理器。      
  15.            例如,getAge方法有一个返回值,因此,需要使用可处理输入输出的RPCMessageReceiver类,      
  16.            而update方法没有返回值,因此,需要使用只能处理输入的RPCInOnlyMessageReceiver类。      
  17.         -->      
  18.       <messageReceivers>        
  19.              <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />        
  20.              <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />        
  21.       </messageReceivers>        
  22.    </service>        
  23. </serviceGroup>     



配置 web.xml


[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>      
  2. <web-app version="2.5"       
  3.     xmlns="http://java.sun.com/xml/ns/javaee"       
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee       
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">      
  7.        
  8.     <servlet>      
  9.         <servlet-name>AxisServlet</servlet-name>      
  10. //注册axis2的servlet      
  11.         <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>      
  12.         <load-on-startup>1</load-on-startup>      
  13.     </servlet>      
  14.               
  15.     <servlet-mapping>      
  16.         <servlet-name>AxisServlet</servlet-name>      
  17.         <url-pattern>/services/*</url-pattern>      
  18.     </servlet-mapping>      
  19. //加载spring的配置文件      
  20.     <context-param>      
  21.       <param-name>contextConfigLocation</param-name>      
  22.       
  23.       <param-value>classpath*:applicationContext.xml</param-value>      
  24.     </context-param>      
  25. //增加spring监听器      
  26.     <listener>      
  27.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      
  28.     </listener>      





测试总结:

经过上述的步骤,配置结束,将项目装载的 Tomcat ,启动 ,浏览器输入http://localhost:8080/AxisSpringDemo/services/listServices 出现如下界面说明我们的服务已经发布成功了












访问 
http://localhost:8080/WebService/services/springService?wsdl
可以查看wsdl (springService为service.xml中service的name)
通过上面的测试可以发现:
加入Spring之后,除了spring的引入以及配置,唯一不同的地方就是 services.xml 的配置发生了变化

三  在maven下与spring整合

原文地址:https://blog.csdn.net/qq_22871607/article/details/54343326

第一步:pom.xml导入axis2的依赖  我用1.6.2版本的

<!--axis2版本指定-->

<axis2.version>1.6.2</axis2.version>

 

<!--axis2 begin-->

<dependency>

    <groupId>org.apache.axis2</groupId>

    <artifactId>axis2</artifactId>

    <version>${axis2.version}</version>

</dependency>

<dependency>

    <groupId>org.apache.axis2</groupId>

    <artifactId>axis2-spring</artifactId>

    <version>${axis2.version}</version>

</dependency>

<dependency>

    <groupId>org.apache.axis2</groupId>

    <artifactId>axis2-transport-http</artifactId>

    <version>${axis2.version}</version>

</dependency>

<dependency>

    <groupId>org.apache.axis2</groupId>

    <artifactId>axis2-transport-local</artifactId>

    <version>${axis2.version}</version>

</dependency>

<dependency>

    <groupId>org.apache.axis2</groupId>

    <artifactId>axis2-xmlbeans</artifactId>

    <version>${axis2.version}</version>

</dependency>

<!--axis2 end-->

第二步:创建要发布的接口与实现类(可以不写接口,直接实现类)

接口:


实现类:(实现类上面加上@Component注解,通过spring扫描)


第三步:修改web.xml的配置文件,添加如下内容

<!-- Axis2 -->

<servlet>

    <servlet-name>AxisServlet</servlet-name>

     <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>

    <load-on-startup>2</load-on-startup>

</servlet>

<servlet-mapping>

    <servlet-name>AxisServlet</servlet-name>

    <url-pattern>/services/*</url-pattern>

</servlet-mapping>


第四步:如果你的项目之前没有配置过spring监听器的话,需要做如下配置

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener> 


第五步:配置spring把axis2交给spring来管理


代码:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <description>Srping 整合axis2</description>
    <!--axis2交給srping來管理-->
    <bean id ="applioationContext" class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder"/>
    <!--配置需要提供的服務,已用注解來代替-->
    <!--<bean id ="serviceServerImpl" class="com.amt.web.service.*impl"></bean>-->

</beans>

第六步:配置services.xml文件


services.xml内容如下


<?xml version="1.0" encoding="UTF-8"?>

<!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象 -->

<service name="ServiceServer">

    <description>axis2</description>

    <!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象 -->

    <parameter name="ServiceObjectSupplier" locked="false">

        org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier

    </parameter>

    <!--

       SpringBeanName固定的不能改

       serviceServerImplspring中注册的实现类得id,@Component注解

     -->

    <parameter name="SpringBeanName">serviceServerImpl</parameter>

    <!--

  <messageReceivers>元素,该元素用于设置处理WebService方法的处理器。

    例如,getGreeting方法有一个返回值,因此,需要使用可处理输入输出的RPCMessageReceiver类,

    update方法没有返回值,因此,需要使用只能处理输入的RPCInOnlyMessageReceiver类。

     -->

    <messageReceivers>

        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"

                         class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>

        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"

                         class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>

    </messageReceivers>

</service>


第七步:部署tomcat并启动服务访问

输入:http://localhost:8181/ROOT/services/ServiceServer?wsdl


传递参数:


到这里wsdl能够浏览并显示出来说明已经配置成功了。

当然如果想要可以看到服务,可以复制axis2包下的


到项目的wabapp下,然后访问:http://localhost:8082/ROOT/axis2-web/



即可看到所有提供的服务了。

整体的结构:


到这里基本的已经全部配置完毕














访问 
http://localhost:8080/WebService/services/springService?wsdl
可以查看wsdl (springService为service.xml中service的name)
通过上面的测试可以发现:
加入Spring之后,除了spring的引入以及配置,唯一不同的地方就是 services.xml 的配置发生了变化

猜你喜欢

转载自blog.csdn.net/liwb94/article/details/79863979