Axis 简单入门实例

原文链接:http://www.cnblogs.com/bin1991/p/3636649.html
下面记录是我的一个Axis的简单入门实例,不足之处,请 勿见怪

Step1:新建一个WebProject,如我的WebProject就是AxisStudy

Step2:下载对就应的axis2-bin的包,拷贝到WEB-INF目录下


Step3:修改Web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<!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>
	<display-name>Apache-Axis</display-name>

	<listener>
		<listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>
	</listener>

	<servlet>
		<servlet-name>AxisServlet</servlet-name>
		<display-name>Apache-Axis Servlet</display-name>
		<servlet-class>
			org.apache.axis.transport.http.AxisServlet
    </servlet-class>
	</servlet>

	<servlet>
		<servlet-name>SOAPMonitorService</servlet-name>
		<display-name>SOAPMonitorService</display-name>
		<servlet-class>
			org.apache.axis.monitor.SOAPMonitorService
    </servlet-class>
		<init-param>
			<param-name>SOAPMonitorPort</param-name>
			<param-value>5001</param-value>
		</init-param>
		<load-on-startup>100</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>AxisServlet</servlet-name>
		<url-pattern>/servlet/AxisServlet</url-pattern>
	</servlet-mapping>

	<servlet-mapping>
		<servlet-name>AxisServlet</servlet-name>
		<url-pattern>*.jws</url-pattern>
	</servlet-mapping>

	<servlet-mapping>
		<servlet-name>AxisServlet</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>

	<servlet-mapping>
		<servlet-name>SOAPMonitorService</servlet-name>
		<url-pattern>/SOAPMonitor</url-pattern>
	</servlet-mapping>
	<session-config>
		<!-- Default to 5 minute session timeouts -->
		<session-timeout>5</session-timeout>
	</session-config>
	<mime-mapping>
		<extension>wsdl</extension>
		<mime-type>text/xml</mime-type>
	</mime-mapping>

	<mime-mapping>
		<extension>xsd</extension>
		<mime-type>text/xml</mime-type>
	</mime-mapping>

	<welcome-file-list id="WelcomeFileList">
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.jws</welcome-file>
	</welcome-file-list>

</web-app>

Step4:编写业务类如下:

package com.service;

public class MyService {

	public String sayHello(String name) {
		return "Welcome to you, " + name;
	}
}

Step5:在WEB-INF下新建文件server-config.wsdd,内容如下

<deployment xmlns="http://xml.apache.org/axis/wsdd/"
	xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
	<handler type="java:org.apache.axis.handlers.http.URLMapper"
		name="URLMapper" />
	<service name="myService" provider="java:RPC">
		<parameter name="className" value="com.service.MyService" />
		<parameter name="allowedMethods" value="*" /> <!--*代表所有方法 -->
	</service>
	<transport name="http">
		<requestFlow>
			<handler type="URLMapper" />
		</requestFlow>
	</transport>
</deployment>

Step6:启动Tomcat,在浏览器的地址框输入:http://localhost:8080/AxisStudy/services(主机地址/项目名称/services),看到如下界面,说明发布成功了


Step7:编写测试类:

import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;



public class Test {
	public static void main(String[] args){

		try {
			String url = "http://localhost:8080/AxisStudy/services/myService";//主机地址/项目名称/services/调用的类
			Service service = new Service();
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(url);
			call.setOperationName(new QName(url, "sayHello"));//调用的方法 
			String str = (String)call.invoke(new Object[] {"bin"});//传参,调用
			System.out.println(str);
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}
}

Step8:得到结果


转载于:https://www.cnblogs.com/bin1991/p/3636649.html

展开阅读全文
博主设置当前文章不允许评论。