Spring+Axis1.4开发WebService实例
本文需要两个eclipse project,一个是web工程,用于webservice服务端,一个是java project,用于客户端
一个基本的axis web服务由以下四部分组成:Axs servlet,Axis部署描述文,远程借口,服务实现
首先看服务端的代码实现(看看这里)
创建POJO接口
package ch16.SimpleAxis;
public interface HelloWorld {
public String getMessage();
}
public interface HelloWorld {
public String getMessage();
}
创建POJO接口实现:
package ch16.SimpleAxis;
public class SimpleHelloWorld implements HelloWorld {
public String getMessage() {
return "hello world axis";
}
}
public class SimpleHelloWorld implements HelloWorld {
public String getMessage() {
return "hello world axis";
}
}
创建远程接口:
package ch16.SimpleAxis;
import&i.Remote;
import&i.RemoteException;
public interface RemoteHelloWorld extends Remote {
public String getMessage() throws RemoteException;
}
import&i.Remote;
import&i.RemoteException;
public interface RemoteHelloWorld extends Remote {
public String getMessage() throws RemoteException;
}
创建WEB服务
我们的应用不需要为服务创建实例,因为这是axis的工作,所以,我们如果简单的实现RemoteHelloWorld,就不能对他进行DI操作
我们可以使用简单的远程接口实现类,但是,这种方法有一种缺点--你不可以使用DI,一个
更好的解决办法就是为你的POJO服务对象做一个简单的包装器,在spring加载ApplicationContext时候可以使用你配置的DI。Spring提ServletEndpoingSupport类可以让这一切变得简单,并且允许你访为web应用而加载的ApplicationContext
package ch16.SimpleAxis;
import&i.RemoteException;
import&l.rpc.ServiceException;
import ing.jaxrpc.ServletEndpointSupport;
//一个JAXRPC的包装器
public class JaxRpcHelloWorld extends ServletEndpointSupport implements
RemoteHelloWorld {
private HelloWorld helloWorld;
protected void onInit() throws ServiceException {
helloWorld=(HelloWorld)getApplicationContext().getBean("helloWorldService");
}
import&i.RemoteException;
import&l.rpc.ServiceException;
import ing.jaxrpc.ServletEndpointSupport;
//一个JAXRPC的包装器
public class JaxRpcHelloWorld extends ServletEndpointSupport implements
RemoteHelloWorld {
private HelloWorld helloWorld;
protected void onInit() throws ServiceException {
helloWorld=(HelloWorld)getApplicationContext().getBean("helloWorldService");
}
public String getMessage() throws RemoteException {
return&Message();
}
}
return&Message();
}
}
这个类有两个重要的方法getMessage()和onInit()方法的实现。在getMessage()中,你看到真实的处理和HelloWorld接口的一个实例相似,HelloWorld接口和RemoteHelloWorld几口根据同样的名称共享方法,但是HelloWorld没有继承Remote,方法也没抛出RemoteException,HelloWorld接口地实现可以在很多环境下通过servlet容器来进行简单的测试,因为他和java远程接口没有关系,我们可可以仅仅使用JaxRpcHelloWorld类代表RemoteHelloWorld,但是将消弱对于RemoteHelloWorld接口的可重用性实现,因为所有的方法必须抛出RemoteExceptio和继Remote,使用HelloWorld接口,我们能够使他在某些环境中更简单的使用
服务端的spring配置文件l
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-2.0.xsd">
<bean id="helloWorldService" class="ch16.SimpleAxis.SimpleHelloWorld">
</bean>
</beans>
<beans
xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-2.0.xsd">
<bean id="helloWorldService" class="ch16.SimpleAxis.SimpleHelloWorld">
</bean>
</beans>
axis部署描述符server-config.wsdd 放在web-inf下
<deployment xmlns="/axis/wsdd/"
调用webservice服务 xmlns:java="/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="ch16.SimpleAxis.JaxRpcHelloWorld"/>
<parameter name="allowedMethods" value="*"/>
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper"/>
</requestFlow>
</transport>
</deployment>
调用webservice服务 xmlns:java="/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="ch16.SimpleAxis.JaxRpcHelloWorld"/>
<parameter name="allowedMethods" value="*"/>
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper"/>
</requestFlow>
</transport>
</deployment>
其中最重要的是service标签,他定义了WS的名字,有两个子标签,一个用来指定服务实现类的全名,一个用来定义服务中要被暴露的服务方法过滤器
指定java:RPC表明这是一个RPC风格的服务
l
加入以下元素
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/l</param-value>
</context-param>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.t.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>axis</servlet-name>
<servlet-class>org.ansport.http.AxisServlet</servlet-class>
<param-name>contextConfigLocation</param-name>
<param-value>/l</param-value>
</context-param>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.t.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>axis</servlet-name>
<servlet-class>org.ansport.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>
</servlet>
<servlet-mapping>
<servlet-name>axis</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
部署这个web服务,我们在浏览器中运行如下地址(根据你自己的webapp)
localhost:81/ProSpringStudyWeb/services 可以看到你定义的web服务如下:
Some Services
∙ HelloWorld (wsdl)
o getMessage
点击wsdl链接可以看到自动生成并保存的wdsl文档
客户端:
首先把服务端的两个HelloWorld接口打包成jar,加载到客户端classpath下,以便使用
使用JaxRpcPortProxyFactoryBean配置一个JAXRPC代理
l
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-2.0.xsd">
<bean id="helloWorldService" class="ing.jaxrpc.JaxRpcPortProxyFactoryBean">
<property name="serviceFactoryClass">
<value>org.apache.axis.client.ServiceFactory</value>
</property>
<property name="wsdlDocumentUrl">
<value>localhost:81/ProSpringStudyWeb/services/HelloWorld?wsdl</value>
<beans
xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-2.0.xsd">
<bean id="helloWorldService" class="ing.jaxrpc.JaxRpcPortProxyFactoryBean">
<property name="serviceFactoryClass">
<value>org.apache.axis.client.ServiceFactory</value>
</property>
<property name="wsdlDocumentUrl">
<value>localhost:81/ProSpringStudyWeb/services/HelloWorld?wsdl</value>
</property>
<property name="namespaceUri">
<value>localhost:81/ProSpringStudyWeb/services/HelloWorld</value>
</property>
<property name="serviceName">
<value>JaxRpcHelloWorldService</value>
</property>
<property name="portName">
<value>HelloWorld</value>
</property>
<property name="portInterface">
<value>ch16.SimpleAxis.RemoteHelloWorld</value>
</property>
<property name="serviceInterface">
<value>ch16.SimpleAxis.HelloWorld</value>
<property name="namespaceUri">
<value>localhost:81/ProSpringStudyWeb/services/HelloWorld</value>
</property>
<property name="serviceName">
<value>JaxRpcHelloWorldService</value>
</property>
<property name="portName">
<value>HelloWorld</value>
</property>
<property name="portInterface">
<value>ch16.SimpleAxis.RemoteHelloWorld</value>
</property>
<property name="serviceInterface">
<value>ch16.SimpleAxis.HelloWorld</value>
</property>
</bean>
</beans>
</bean>
</beans>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论