axis2+spring集成

来源:互联网 发布:淘宝注销账号怎么注销 编辑:程序博客网 时间:2024/04/19 03:30

一、下载jar包

    axis2-1.6.3下载:

    下载地址:http://axis.apache.org/axis2/Java/core/download.cgi,

    下载axis2-1.6.3-bin.zip,此压缩包包含了所有axis2的jar

二、创建整合工程并发布测试

    1.在搭好的spring MVC框架中,引入axis2的jar

    2.在WEB-INF下创建services文件夹,名字一定是services,其他不能识别,在services一定要再创建一个文件夹(名字顺便,例如:axis),        在axis下创建一个文件夹,名字一定是META-INF,在META-INF下创建services.xml文件和一个空的ServiceData.xml,这个名字也是不变的。

 最后路径是WEB-INF/services/axis/META-INF/services.xml services.xml代码如下:

3、services.xml

<?xml version="1.0" encoding="UTF-8"?>  <serviceGroup>           <!-- name属性配置WebService的名称 -->         <service name="DataSyncService">                  <description>Web Service</description>                  <!-- ServiceClass属性配置提供WebService服务类的全类名 -->                <parameter name="ServiceObjectSupplier"><!-- 固定的不变 -->             org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier       </parameter>       <parameter name="SpringBeanName">dataSync</parameter><!-- bean名字 -->               <messageReceivers>                         <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />                         <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />                  </messageReceivers>           </service>    </serviceGroup>   
4、添加axis_webservice.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd        http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd">            <bean id="applicationContext"        class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />    <bean id="dataSync" class="com.tjhq.webservice.impl.DataSyncImpl"></bean></beans>
5、添加接口

package com.tjhq.webservice;import javax.jws.WebParam;public interface DataSync {public String sayHello(@WebParam(name = "arg0") String text);}
6、实现接口
package com.tjhq.webservice.impl;import com.tjhq.webservice.DataSync;public class DataSyncImpl implements DataSync {@Overridepublic String sayHello(String text) {return "Hello : " + text;}}
7、配置web.xml

<!-- 加载spring容器 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/applicationContext-*.xml,classpath:webservice/axis_webservice.xml,classpath:activemq/activemq.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
服务端配置完成



客户端调用代码

package com.tjhq.webservice;import javax.xml.namespace.QName;import org.apache.axiom.om.OMElement;import org.apache.axis2.AxisFault;import org.apache.axis2.addressing.EndpointReference;import org.apache.axis2.client.Options;import org.apache.axis2.rpc.client.RPCServiceClient;import org.apache.cxf.endpoint.Client;import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;public class WebservicesClient {public static void main(String[] args) {axisCallAxis();}/** * cfx 调用axis的webservices */public static void cfxCallAxis() {ClassLoader cl = Thread.currentThread().getContextClassLoader();JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();Client client = dcf.createClient("http://localhost:7001/efmisweb/services/DataSyncService?wsdl");Thread.currentThread().setContextClassLoader(cl);try {// invoke第一个参数是方法名称,第二个是参数Object[] objects = client.invoke("sayHello", "sayHello");System.out.println("返回对象的长度:" + objects.length);System.out.println(objects[0].toString());} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * axis 调用axis的webservices */public static void axisCallAxis() {RPCServiceClient serviceClient;try {serviceClient = new RPCServiceClient();Options options = serviceClient.getOptions();EndpointReference targetEPR = new EndpointReference("http://localhost:7001/efmisweb/services/DataSyncService"); // 这个写自己生成的webservice地址options.setTo(targetEPR);Object[] send = new Object[] { "zzz" };// the type of send// 指定getPrice方法返回值的数据类型的Class对象Class[] returnTypes = new Class[] { String.class };QName qName = new QName("http://impl.webservice.tjhq.com", "sayHello");// 调用方法一 传递参数,调用服务,获取服务返回结果集OMElement element = serviceClient.invokeBlocking(qName, send);// 值得注意的是,返回结果就是一段由OMElement对象封装的xml字符串。String result = element.getFirstElement().getText();System.out.println(result);} catch (AxisFault e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

参考资料:

http://www.cnblogs.com/yuxuan/p/4028359.html
http://blog.csdn.net/xuxile/article/details/50073007


0 0
原创粉丝点击