首页 > 代码库 > Spring集成axis2

Spring集成axis2

1.新建一个项目,结构如下

2.引入项目所需jar包

axis相关jar文件说明请查阅该博文

 

3.配置web.xml,注册axis2信息

 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   <display-name>Spring+axis2</display-name>     9     <!-- 整合spring -->10     <listener>11         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>12     </listener>13      <context-param>14         <param-name>contextConfigLocation</param-name>15         <param-value>/WEB-INF/config/appContext*.xml</param-value>16     </context-param>17     <!-- 注册axis2的servlet -->18    <servlet>19        <servlet-name>AxisServlet</servlet-name>20        <servlet-class>21            org.apache.axis2.transport.http.AxisServlet22        </servlet-class>23        <load-on-startup>1</load-on-startup>24    </servlet>25    <servlet-mapping>26        <servlet-name>AxisServlet</servlet-name>27        <url-pattern>/services/*</url-pattern>28    </servlet-mapping>    29     30     31   <welcome-file-list>32     <welcome-file>index.jsp</welcome-file>33   </welcome-file-list>34 </web-app>

3.创建服务接口

 1 package com.chinawu.service; 2 /** 3  *  4  * @ClassName: IWebService  5  * @Description: WebService接口类  6  * @author 吴宇斌 7  * @date 2014-8-7 下午09:40:19 8  * @version 1.0 9  */10 public interface IWebService {11     public String welcome();12     public String getParamName(String str);13 }

4.创建服务接口实现类

 1 package com.chinawu.service; 2 /** 3  *  4  * @ClassName: WeService  5  * @Description: WebService实现类 6  * @author 吴宇斌 7  * @date 2014-8-7 下午09:41:36 8  * @version 1.0 9  */10 public class WebService implements IWebService{11 12     public String getParamName(String str) {13         return "传入参数:"+str;14     }15 16     public String welcome() {17         return "welcome chinaWu!";18     }19     20 }

5.创建spring配置文件

1 <?xml version="1.0" encoding="UTF-8"?>2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"3 "http://www.springframework.org/dtd/spring-beans.dtd">4 <beans>5     <bean id="webService" class="com.chinawu.service.WebService"></bean>6 </beans>

6.创建axis配置文件

<?xml version="1.0" encoding="UTF-8"?><service name="WebService">    <description>Spring+axis2</description>        <parameter name="ServiceClass">            com.chinawu.service.WebService        </parameter>    <parameter name="SpringBeanName">webService</parameter>    <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>

7.服务访问

http://localhost/spring2axis/services/WebService?wsdl

8.客户端接口调用

package com.chinawu.client;import javax.xml.namespace.QName;import org.apache.axis2.AxisFault;import org.apache.axis2.addressing.EndpointReference;import org.apache.axis2.client.Options;import org.apache.axis2.rpc.client.RPCServiceClient;public class Client {     public static void main(String[] args1) throws AxisFault {                        EndpointReference targetEPR = new EndpointReference("http://localhost/spring2axis/services/WebService");            RPCServiceClient serviceClient = new RPCServiceClient();            Options options = serviceClient.getOptions();            options.setTo(targetEPR);            QName opAddDevice = new QName("http://service.chinawu.com", "welcome");            Object[] opGetArgs = new Object[] {};            Class[] returnTypes = new Class[] { String.class };            Object[] response = serviceClient.invokeBlocking(opAddDevice,opGetArgs, returnTypes);            String  result = (String)response[0];            System.out.println("WebService-welcome-reutnInfo:"+result);                        opAddDevice = new QName("http://service.chinawu.com", "getParamName");            opGetArgs = new Object[] {"你好!"};            returnTypes = new Class[] { String.class };            response = serviceClient.invokeBlocking(opAddDevice,opGetArgs, returnTypes);            result = (String)response[0];            System.out.println("WebService-getParamName-reutnInfo:"+result);        }}

输出结果信息

9.查看服务列表

http://localhost/spring2axis/services/listServices

至此spring成功集成axis,项目源码下载地址: http://pan.baidu.com/s/1gd8BwYn