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);
}
}