使用版本:
eclipse:eclipse-jee-kepler-R-win32.zip
JDK:jdk-7u25-windows-i586.exe j2sdk1.4.2_06.rar
Myeclipse:myeclipse-8.5.0-win32.exe
Tomcat:apache-tomcat-7.0.42-windows-x86.zip apache-tomcat-5.0.12.zip
Axis2:axis2-1.6.2-bin.zip axis2-1.6.2-war.zip axis2-eclipse-codegen-plugin-1.6.2.zip axis2-eclipse-service-plugin-1.6.2.zip
Axis1:axis-1.4.rar
WebLogic:oepe-indigo-installer-12.1.1.0.1.201203120349-12.1.1-win32.exe
Spring:spring-framework-3.1.1.RELEASE-with-docs.zip
Hibernate:hibernate-distribution-3.6.10.Final-dist.zip
Structs2;struts-2.3.15.1-all.zip
SVN:eclipse-svnplugin(subclipse site-1.6.2)
Oracle9i:Oracle 9i Disk1.iso Oracle 9i Disk2.iso Oracle 9i Disk3.iso
一、服务端
1.1 创建Dynamic Web Project
将axis2和spring的JAR包拷贝到工程LIB目录:
1.2 添加代码
public interface HelloWorldRemote {
public String getMessage(String name);
}
package com.server;
public class HelloWorldBean implements HelloWorldRemote {
private String helloStr; // Spring 中需要注入的字符串
public String getHelloStr() {
return helloStr;
}
public void setHelloStr(String helloStr) {
this.helloStr = helloStr;
}
// 实现接口中的方法
public String getMessage(String name) {
return helloStr + ":" + name;
}
}
package com.server;
import javax.xml.rpc.ServiceException;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport;
public class HelloWorldWebService extends ServletEndpointSupport {
private HelloWorldRemote helloWorld;
protected void onInit() throws ServiceException {
// 在 Spring 容器中获取 Bean 的实例
helloWorld = (HelloWorldRemote) getApplicationContext().getBean("myHelloWorldBean");
}
public String getMessage(String name) {
// 执行 Bean 中的相同的方法
return helloWorld.getMessage(name);
}
}
1.3 配置
web.xml+applicationContext.xml+server-config.wsdd
1.3.1 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
<display-name>spring-axis2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!--Spring 框架需要引入的配置文件及相关类 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!--axis 需要引入的 Servlet -->
<servlet>
<servlet-name>axis</servlet-name>
<servlet-class>
org.apache.axis.transport.http.AxisServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>axis</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<!--axis 的 Web Service 的 Web 发布路径 -->
</web-app>
1.3.2 新建applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="myHelloWorldBean" class="com.server.HelloWorldBean">
<property name="helloStr">
<value>Say Hello to :</value>
</property>
</bean>
</beans>
1.3.3 新建
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<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>
<!-- 自定义服务 -->
<service name="myWebService" provider="java:RPC">
<parameter name="className"
value="com.server.HelloWorldWebService" />
<parameter name="allowedMethods" value="*" />
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper" />
</requestFlow>
</transport>
</deployment>
1.4 启动服务
http://localhost:8080/spring-axis2/services/SpringAxis2WebService
http://localhost:8080/spring-axis2/services
二、客户端
package com.client;
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class TestWebServiceClient {
public static void main(String[] args) throws Exception {
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/spring-axis/services/myWebService?wsdl");
options.setTo(targetEPR);
//
QName opAddEntry = new QName("http://localhost:8080/spring-axis/services/myWebService", "getMessage");
Object[] opAddEntryArgs = new Object[] { "ABC" };
try {
String str = (String) serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs,
new Class[] { String.class })[0];
System.out.println(str);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
运行:
log4j:WARN No appenders could be found for logger (org.apache.axis2.context.AbstractContext).
log4j:WARN Please initialize the log4j system properly.
Say Hello to ::ABC