WebService框架整理(二) Axis1+Spring

初识Axis1就要把它集成到Spring框架上。一方面是当时的项目要求,在一方面更是我对于Spring情有独钟。
Axis1+Spring比较简单,这种便利得益于Spring的ServletEndpointSupport类支持。

相关链接:
WebService框架整理(一) Axis1
WebService框架整理(二) Axis1+Spring

我们将用到以下Jar:
引用

activation.jar
axis.jar
commons-discovery.jar
commons-logging.jar
jaxrpc.jar
log4j-1.2.15.jar
mail.jar
wsdl4j.jar
spring.jar

主要就是加入了spring.jar包!

再看看web.xml,加入了Spring的相关内容。大家都熟悉Spring,我就不废话了!
<?xml version="1.0" encoding="UTF-8"?>
<web-app
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID"
	version="2.5">
	<display-name>spring-axis-1</display-name>
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:log4j.xml</param-value>
	</context-param>
	<context-param>
		<param-name>log4jRefreshInterval</param-name>
		<param-value>60000</param-value>
	</context-param>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>
	<filter>
		<filter-name>UTF-8 Filter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>UTF-8 Filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<servlet>
		<display-name>Apache-Axis Servlet</display-name>
		<servlet-name>axis</servlet-name>
		<servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
		<load-on-startup>0</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>axis</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>
</web-app>


我们定义一个用于计算的CalcService接口及其实现CalcServiceImpl,如下:
/**
 * 简单计算
 * 
 * @author 梁栋
 * @version 1.0
 * @since 1.0
 */
public interface CalcService {

	/**
	 * 求和
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	int add(int a, int b);

}

给出对应的实现内容:
import org.zlex.axis.service.CalcService;

/**
 * 计算
 * 
 * @author 梁栋
 * @version 1.0
 * @since 1.0
 */
public class CalcServiceImpl implements CalcService {

	@Override
	public int add(int a, int b) {
		return a + b;
	}

}

再简单不过的1+1问题!
将其注入到Spring的容器中,applicationContext.xml如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<bean
		id="calcService"
		class="org.zlex.axis.service.impl.CalcServiceImpl" />
</beans>

作为spring与axis1对接,需要做一个ServletEndpointSupport继承实现WebService,如下所示:
import javax.xml.rpc.ServiceException;

import org.springframework.context.ApplicationContext;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport;
import org.zlex.axis.service.CalcService;

/**
 * WebService入口
 * 
 * @author 梁栋
 * @version 1.0
 * @since 1.0
 */
public class WebService extends ServletEndpointSupport {

	private ApplicationContext applicationContext;
	private CalcService calcService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.remoting.jaxrpc.ServletEndpointSupport#onInit()
	 */
	@Override
	protected void onInit() throws ServiceException {
		// 初始化Spirng 配置
		applicationContext = super.getApplicationContext();

		calcService = (CalcService) applicationContext.getBean("calcService");

	}

	/**
	 * 求和
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public String add(int a, int b) {
		return String.valueOf(calcService.add(a, b));
	}

}

这里为了便于在eclipse演示,将返回值定为String类型!
现在我们将该服务植入Axis中,修改server-config.wsdd文件,在原文件中加入如下内容:
	<!-- 自定义服务 -->
	<service
		name="WebService"
		provider="java:RPC">
		<parameter
			name="className"
			value="org.zlex.axis.WebService" />
	</service>

修改后的server-config.wsdd文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<deployment
	xmlns="http://xml.apache.org/axis/wsdd/"
	xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
	<globalConfiguration>
		<parameter
			name="adminPassword"
			value="admin" />
		<parameter
			name="sendXsiTypes"
			value="true" />
		<parameter
			name="sendMultiRefs"
			value="true" />
		<parameter
			name="sendXMLDeclaration"
			value="true" />
		<parameter
			name="axis.sendMinimizedElements"
			value="true" />
		<requestFlow>
			<handler
				type="java:org.apache.axis.handlers.JWSHandler">
				<parameter
					name="scope"
					value="session" />
			</handler>
			<handler
				type="java:org.apache.axis.handlers.JWSHandler">
				<parameter
					name="scope"
					value="request" />
				<parameter
					name="extension"
					value=".jwr" />
			</handler>
		</requestFlow>
	</globalConfiguration>
	<handler
		name="Authenticate"
		type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" />
	<handler
		name="LocalResponder"
		type="java:org.apache.axis.transport.local.LocalResponder" />
	<handler
		name="URLMapper"
		type="java:org.apache.axis.handlers.http.URLMapper" />
	<service
		name="AdminService"
		provider="java:MSG">
		<parameter
			name="allowedMethods"
			value="AdminService" />
		<parameter
			name="enableRemoteAdmin"
			value="false" />
		<parameter
			name="className"
			value="org.apache.axis.utils.Admin" />
		<namespace>http://xml.apache.org/axis/wsdd/</namespace>
	</service>
	<service
		name="Version"
		provider="java:RPC">
		<parameter
			name="allowedMethods"
			value="getVersion" />
		<parameter
			name="className"
			value="org.apache.axis.Version" />
	</service>
	<transport
		name="http">
		<requestFlow>
			<handler
				type="URLMapper" />
			<handler
				type="java:org.apache.axis.handlers.http.HTTPAuthHandler" />
		</requestFlow>
	</transport>
	<transport
		name="local">
		<responseFlow>
			<handler
				type="LocalResponder" />
		</responseFlow>
	</transport>

	<!-- 自定义服务 -->
	<service
		name="WebService"
		provider="java:RPC">
		<parameter
			name="className"
			value="org.zlex.axis.WebService" />
	</service>
</deployment>

我们随机抽取2个数进行求和运算,并验证WebService和本地计算结果是否一致,测试用例WebServiceTest如下:
/**
 * WebService测试
 * 
 * @author 梁栋
 * @version 1.0
 * @since 1.0
 */
public class WebServiceTest {
	private String nameSpaceUri = "http://localhost:8080/axis/services/WebService";
	private String wsdlUrl = nameSpaceUri + "?wsdl";

	private Service service;
	private Call call;

	@Before
	public final void init() throws Exception {
		// 创建调用对象
		service = new Service();
		call = (Call) service.createCall();

		// 调用 远程方法
		call.setOperationName(new QName(nameSpaceUri, "add"));

		// 设置URL
		call.setTargetEndpointAddress(new URL(wsdlUrl));
	}

	@Test
	public final void testAdd() throws Exception {
		
		// 设置参数
		Random rnd = new Random();

		int a = rnd.nextInt(100);

		int b = rnd.nextInt(100);

		// 执行远程调用,同时获得返回值
		String r = (String) call.invoke(new Object[] { a, b });

		assertEquals(String.valueOf(a + b), r);
		System.out.println("a(" + a + ") + b(" + b + ") = " + r);

	}
}

我们验证一下结果!
WebService框架整理(二) Axis1+Spring_第1张图片
顺利通过!
我们在通过Eclipse验证一下这个服务!

WebService框架整理(二) Axis1+Spring_第2张图片
Eclipse中输入参数验证WebService,如果要看到返回值就需要把返回值定为String类型。如果用int类型,我们只能通过测试用例检测这个结果!

完整项目实例见附件!

相关链接:
WebService框架整理(一) Axis1
WebService框架整理(二) Axis1+Spring

你可能感兴趣的:(eclipse,spring,框架,xml,webservice)