webservice-axis2方式 maven + spring

首先建议阅读官方文档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

猜你喜欢

转载自blog.csdn.net/u013278314/article/details/80257789