1.新建一个web工程
2.将Axis-War包解压将WEB-INF/下的conf,modules,services,lib拷贝到工程WebRoot/WEB-INF/如上图所示
3.修改web.xml
View Code
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>AxisServlet</servlet-name>
<display-name>Apache-Axis Servlet</display-name>
<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
<!--<init-param>-->
<!--<param-name>axis2.xml.path</param-name>-->
<!--<param-value>/WEB-INF/conf/axis2.xml</param-value>-->
<!--<param-name>axis2.xml.url</param-name>-->
<!--<param-value>http://localhost/myrepo/axis2.xml</param-value>-->
<!--<param-name>axis2.repository.path</param-name>-->
<!--<param-value>/WEB-INF</param-value>-->
<!--<param-name>axis2.repository.url</param-name>-->
<!--<param-value>http://localhost/myrepo</param-value>-->
<!--</init-param>-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>AxisAdminServlet</servlet-name>
<display-name>Apache-Axis AxisAdmin Servlet (Web Admin)</display-name>
<servlet-class>
org.apache.axis2.webapp.AxisAdminServlet</servlet-class>
</servlet>
<!-- servlet>
<servlet-name>SOAPMonitorService</servlet-name>
<display-name>SOAPMonitorService</display-name>
<servlet-class>org.apache.axis2.soapmonitor.servlet.SOAPMonitorService</servlet-class>
<init-param>
<param-name>SOAPMonitorPort</param-name>
<param-value>5001</param-value>
</init-param>
<load-on-startup>1</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>AxisAdminServlet</servlet-name>
<url-pattern>/axis2-admin/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
4.运行工程访问 http://localhost:8080/axis2/services/Version?wsdl
5.发布service
View Code
package com.service;
/**
*<pre>
* Copyright (c)
* @Description: helloworldservice
* @author
* @date: Mar 28, 20121:50:34 PM
* @verison 1.0
* @modify Mar 28, 20121:50:34 PM
*</pre>
**/
public class HelloWorldService {
public String sayHi(String helloName){
return "hi "+helloName;
}
}
将helloworldService生成arr文件详情请看
http://www.cnblogs.com/ywChen/articles/2426494.html
将arr文件生成在WebRoot/WEB-INF/services
运行工程访问
http://localhost:8080/axis2project/services/HelloWorldService?wsdl
6.客户端访问
View Code
package com.client;
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
/**
*<pre>
* Copyright (c) 2010
* @Description: 客户端访问service
* @author
* @date: Mar 28, 20121:52:43 PM
* @verison 1.0
* @modify Mar 28, 20121:52:43 PM
*</pre>
**/
public class HelloWorldServiceClient {
public static void main(String args[]) throws AxisFault{
//RPCServiceClient是RPC方式调用
RPCServiceClient client = new RPCServiceClient();
Options options = client.getOptions();
//设置调用WebService的URL
String address = "http://localhost:8080/axis2project/services/HelloWorldService";
EndpointReference epf = new EndpointReference(address);
options.setTo(epf);
/**
* 设置将调用的方法,http://ws.apache.org/axis2是方法
* 默认(没有package)命名空间,如果有包名
* 就是http://service.hoo.com 包名倒过来即可 空间名应查看wsdl的空间名targetNamespace="http://service.com"
* sayHello就是方法名称了
*/
QName qname = new QName("http://service.com", "sayHi");
//指定调用的方法和传递参数数据,及设置返回值的类型
Object[] result = client.invokeBlocking(qname, new Object[] { "yuwen" }, new Class[] { String.class });
System.out.println(result[0]);
}
}
备注:
发布可以不生成arr文件 在/WEB-INF/services新建HelloWorldService目录如下所示
services.xml
View Code
<?xml version="1.0" encoding="UTF-8"?>
<service name="HelloWorldService">
<description>HelloWorldService</description>
<parameter name="ServiceClass">com.service.HelloWorldService</parameter>
<operation name="sayHi">
<messageReceiver
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>