使用Axis2框架实现webservice

如今很多项目规模逐步增加,项目可能分为不同的子系统,各个项目独立运行,相互之间也有联系,各个系统之间的通信问题有多种方式,webservice就是其中之一,通过webservice可以在两个相互独立运行的系统之间进行通信,webservice的实现方式有多种,这里介绍的是使用 Apache 的 Axis2 框架来实现 webservice,使开发过程和难度降低了很多,这里简要介绍一下在spring整合 Axis2 框架实现 webservice 实现服务提供端,大致步骤如下;

1,建立一个spring 的动态 web 项目;
2,准备 Axis2 相关的jar包
,注意在项目的builPath中添加相关的Axis2的jar包,包有:axiom-api-1.2.20.jar,xxiom-impl-1.2.20.jar,axis2-adb-1.7.4.jar, axis2-kernel-1.7.4.jar,axis2-spring-1.7.4.jar,axis2-transport-http-1.7.4.jar, axis2-transport-local-1.7.4.jar,commons-codec-1.3.jar,commons-httpclient-3.1.jar, commons-logging-1.1.1.jar,dom4j-1.6.1.jar,neethi-3.0.3.jar,stax2-api-3.1.1.jar, woden-core-1.0M10.jar,woodstox-core-asl-4.2.0.jar,wsdl4j-1.6.2.jar,xmlschema-core-2.2.1.jar 注意可能有更高版本,或者在项目中引入对应的依赖;
3,在web项目的web.xml中配置spring和axis2相关的servlet;axis2的配置如下所示;
4,webservice相关实现,包含(1)业务接口定义,(2)业务接口实现,(3)spring和axis2的适配器实现,即实现ServiceObjectSupplier, ApplicationContextAware两个接口的类;
5,在WEB-INF下合适文件夹位置建立Axis2的配置文件services.xml,如下所示;
6,spring的配置文件applicationContext.xml加载相关的bean,如用户的业务接口实现的bean,axis2的ApplicationContextHolder辅助bean等等,如下所示;

7,发布到tomcat中,在浏览器访问wsdl测试结果,例如本例为 http://localhost:8080/J2EE021/services/productWebService?wsdl 

步骤内容涉及相关:

3步骤涉及相关,web.xml中仅axis2的配置内容如下,

<!--3步骤涉及相关,web.xml中仅axis2的配置内容如下-->
<servlet>
	<servlet-name>Axis2Service</servlet-name>
	<servlet-class>
	  org.apache.axis2.transport.http.AxisServlet
	</servlet-class>
	<!-- 加载顺序,1代表tomcat最先加载该servlet -->
	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>Axis2Service</servlet-name>
	<url-pattern>/service/*</url-pattern>
	<!--这与services.xml中的webservice定义的service的name属性值组合形成对外的WSDL请求地址-->
</servlet-mapping>

4步骤涉及相关,适配器编写,

/**
 * 4步骤涉及相关
 * @author shenzhenNBA
 * @since 2018.05.16
 */
package com.qyh01.webservice;

import org.apache.axis2.AxisFault;
import org.apache.axis2.ServiceObjectSupplier;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.Parameter;
import org.apache.axis2.i18n.Messages;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author Administrator
 * @attend 1,作用或功能是提供Axis2与spring容器(上下文)的适配器作用,
 * 也即通过该类代码,使axis2通过其配置文件找到已经加载在spring容器中指定的bean;
 * 因此该类功能具有公共功能作用,多个不同的webservice可以共用该适配器;
 * 2,注意需要把axis2相关jar包添加到项目buildPath中;
 * 3,生成客户端时JDK版本低不应于1.6.0_17;
 */

public class ProductWebService implements ServiceObjectSupplier
	,ApplicationContextAware,java.io.Serializable {
	private static final long serialVersionUID = -2600654086023818394L;
	private ApplicationContext appCtx;  
	
	@Override
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		appCtx = arg0; //得到spring的上下文		
	}

	@Override
	public Object getServiceObject(AxisService arg0) throws AxisFault {
		/*
		 在spring的上下文中找出在axis2的配置文件中指定名称的bean,
		 注意:该bean已经通过spring配置文件加载到spring的上下文中
		*/
		Parameter springBeanName = arg0.getParameter("SpringBeanName"); 
		String beanName = ((String)springBeanName.getValue()).trim();  
		if(beanName != null) {
			if(appCtx == null) {  
                throw new AxisFault("ApplicationContext is null");  
            }  
			if(appCtx.getBean(beanName) == null) {  
                throw new AxisFault("axis2 can't find Spring Bean :" + beanName);  
            }  
			return appCtx.getBean(beanName);  
		} else {  
            throw new AxisFault(Messages.getMessage("paramIsNotSpecified", "SERVICE_SPRING_BEANNAME"));  
        }  
	}
}

5步骤涉及相关,axis2适配器配置文件

<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
<!--5步骤涉及相关-->
<!-- 
多个webservice定义使用serviceGroup标签组合,
多个webservice的配置其实可以放到一个配置XML文件中,而为了相对逻辑分清楚可分为不同的配置放不同的目录存放;
-->
<service name="productWebService">  
	<!-- service标签的name属性值作为部分URL,结合web.xml的配置路径和这里值构成完整的WSDL地址,例如本例地址如下 -->
	<!-- http://localhost:8080/J2EE021/services/productWebService?wsdl -->
    <description>axis2与spring集成</description>  
    <parameter name="ServiceObjectSupplier">  
        org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier  
    </parameter>
    <parameter name="SpringBeanName">  
        productService
    </parameter> 
    <messageReceiver>  
        <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" />  
    </messageReceiver>  
</service> 

<service name="productCenterWebService">  
	<!-- service标签的name属性值作为部分URL,结合web.xml的配置路径和这里值构成完整的WSDL地址,例如本例地址如下 -->
	<!-- http://localhost:8080/J2EE021/services/productCenterWebService?wsdl -->
    <description>axis2与spring集成</description>  
    <parameter name="ServiceObjectSupplier">  
        org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier  
    </parameter>  
    <parameter name="SpringBeanName">  
        productCenterService
    </parameter> 
    <messageReceiver>  
        <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" />  
    </messageReceiver>  
</service> 
</serviceGroup>

6步骤涉及相关,spring的配置文件applicationContext.xml中部分Bean配置

<beans ...>
	<!-- 6步骤涉及相关,spring的配置文件applicationContext.xml中部分Bean配置-->
	<bean id="applicationContext" class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />  
	<bean id="userService" class="com.qyh01.service.impl.UserServiceImpl"></bean> 
	<bean id="productService" class="com.qyh01.service.impl.ProductServiceImpl"></bean>
	<bean id="productCenterService" class="com.qyh01.service.impl.ProductCenterServiceImpl"></bean>
</beans>
怎么样,不妨试一下吧,好了,懒人计划到此,欢迎拍砖讨论...

猜你喜欢

转载自blog.csdn.net/shenzhennba/article/details/80345133