Axis+Spring
Axis1整合Spring比较简单,这种便利得益于Spring的ServletEndpointSupport类支持
1、导入jar包
1.1、axis1的jar包
activation.jar
axis.jar
commons-discovery.jar
commons-logging.jar
jaxrpc.jar
log4j-1.2.8.jar
mailapi_1_3_1.jar
wsdl4j-1.5.1.jar
1.2、spring相关jar包
2、创建服务端
2.1、创建接口
package com.axis.service;
public interface HelloWorld {
public String getMessage(String message);
}
2.2、创建实现类
package com.axis.service.impl;
import com.axis.service.HelloWorld;
public class HelloWorldImpl implements HelloWorld {
@Override
public String getMessage(String message) {
return "---------Axis Server-------" + message;
}
}
2.3、创建远程接口
package com.axis.service;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RemoteHelloWorld extends Remote {
public String getMessage(String message) throws RemoteException;
}
2.3、创建WEB服务(spring与axis1对接,需要做一个ServletEndpointSupport继承实现WebService)
package com.axis.service;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport;
public class JaxRpcHelloWorld extends ServletEndpointSupport implements RemoteHelloWorld {
private HelloWorld helloWorld;
protected void onInit() throws ServiceException {
helloWorld = (HelloWorld) getApplicationContext().getBean("helloWorldService");
}
public String getMessage(String message) throws RemoteException {
return helloWorld.getMessage(message);
}
}
这个类有两个重要的方法getMessage()和onInit()方法的实现。在getMessage()中,你看到真实的处理和HelloWorld接口的一个实例相似,HelloWorld接口和RemoteHelloWorld几口根据同样的名称共享方法,但是HelloWorld没有继承Remote,方法也没抛出RemoteException,HelloWorld接口地实现可以在很多环境下通过servlet容器来进行简单的测试,因为他和java远程接口没有关系,我们可可以仅仅使用JaxRpcHelloWorld类代表RemoteHelloWorld,但是将消弱对于RemoteHelloWorld接口的可重用性实现,因为所有的方法必须抛出RemoteExceptio和继Remote,使用HelloWorld接口,我们能够使他在某些环境中更简单的使用
2.4、配置服务端
2.4.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 接口的具体实现类 -->
<bean id="helloWorldService" class="com.axis.service.impl.HelloWorldImpl" />
</beans>
2.4.2、部署axis的WebService服务,在web-inf下加入server-config.wsdd
<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="HelloWorld" provider="java:RPC">
<parameter name="className" value="com.axis.service.JaxRpcHelloWorld" />
<parameter name="allowedMethods" value="*" />
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper" />
</requestFlow>
</transport>
</deployment>
其中最重要的是service标签,他定义了WS的名字,有两个子标签,一个用来指定服务实现类的全名,一个用来定义服务中要被暴露的服务方法过滤器
指定java:RPC表明这是一个RPC风格的服务
2.4.3、配置web.xml
<?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"
version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- axis -->
<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>
部署这个web服务,我们在浏览器中运行如下地址(根据你自己的webapp)
http://localhost:8080/services 可以看到你定义的web服务如下:
点击wsdl链接可以看到wdsl
3、客户端测试
package com.axis.client;
import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import org.apache.axis.client.Service;
import org.junit.Before;
import org.junit.Test;
public class AxisClientTest {
private String nameSpaceUri = "http://localhost:8080/services/HelloWorld";
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, "getMessage"));
// 设置URL
call.setTargetEndpointAddress(wsdlUrl);
}
@Test
public final void testGetMessage() throws Exception {
// 设置参数
String message = "HelloWorld";
// 执行远程调用,同时获得返回值
String str = (String) call.invoke(new Object[] { message });
System.out.println(str);
}
}
