webservices系列(五)——javaweb整合Axis2及多service配置

1.新建一个项目动态web项目webservice_test3

2.打开<Tomcat安装目录>webapps/axis2/WEB-INF。将libconfmodules三个目录复制,并粘贴到webservice_test3项目下的WEB-INF目录下。

3.新建service配置文件,在webservice_test3项目下的WEB-INF目录创建services目录,在其下新建myservice目录(名字可任意)。再myservice目录下新建META-INF目录,再在新建的META-INF目录下创建services.xml

4.写service方法。在src下新建com.lc.service包。并在其下新建NumService.java类。

代码例如以下:

/**
 * @author luocheng
 * 測试webservice实现类
 */
public class NumService {	
	public Integer getSum(Integer num1, Integer num2) {
		return num1 + num2;
	}
}
5.配置service,打开第3步新建的services.xml文件,代码例如以下:

<!-- name是url将訪问的地址 -->
<service name="AxisService">
	<description>AxisService</description>
	<parameter name="ServiceClass">
		com.lc.service.NumService
	</parameter>
	<operation name="sum">
		<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
	</operation>
</service>
web.xml配置文件下增加例如以下代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://Java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
	<servlet>
		<servlet-name>AxisService2</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的訪问路径
		servlet-name:要和上面的servle-name同样
		url-pattern:是URL訪问该servlet时的路径前缀
	 -->
	<servlet-mapping>
		<servlet-name>AxisService2</servlet-name>
		<url-pattern>/service/*</url-pattern>
	</servlet-mapping>
</web-app>
6.公布项目,訪问http://localhost:8080/webservice_test3/service/AxisService?wsdl

多个service类的配置方法

services.xml<service>外套上<serviceGroup></serviceGroup>,就能够写多个service了。

<serviceGroup>
  <!--service1配置-->
  <service>...</service>
  <!--service2配置-->
  <service>...</service>
</serviceGroup>
实例配置例如以下

<!-- 配置service路径 -->
<serviceGroup>
	<!-- name是url将訪问的地址 -->
	<service name="AxisService">
		<description>AxisService</description>
		<parameter name="ServiceClass">
			com.lc.service.NumService
		</parameter>
		<operation name="sum">
			<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
		</operation>
		<operation name="dVal">
			<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
		</operation>
	</service>
	<service name="TestService2">
		<description>TestService2</description>
		<parameter name="ServiceClass">
			com.lc.service2.TestService2
		</parameter>
		<operation name="append">
			<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
		</operation>
	</service>
</serviceGroup>


posted @ 2018-03-02 13:47  zhchoutai  阅读(268)  评论(0编辑  收藏  举报