《pro Spring》学习笔记之Spring+Axis1.4开发WebService实例

来源:互联网 发布:数据录入系统 编辑:程序博客网 时间:2024/05/29 14:39

本文需要两个eclipse project,一个是web工程,用于webservice服务端,一个是java project,用于客户端

一个基本的axis web服务由以下四部分组成:Axs servlet,Axis部署描述文,远程借口,服务实现

首先看服务端的代码实现

创建POJO接口

package ch16.SimpleAxis;
public interface HelloWorld {
    
public String getMessage();
}

 创建POJO接口实现:

 

package ch16.SimpleAxis;
public class SimpleHelloWorld implements HelloWorld {
    
public String getMessage() {
        
return "hello world axis";
    }

}

创建远程接口:

 

package ch16.SimpleAxis;

import java.rmi.Remote;
import java.rmi.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 java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

import org.springframework.remoting.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 helloWorld.getMessage();
    }

}

这个类有两个重要的方法getMessage()和onInit()方法的实现。在getMessage()中,你看到真实的处理和HelloWorld接口的一个实例相似,HelloWorld接口和RemoteHelloWorld几口根据同样的名称共享方法,但是HelloWorld没有继承Remote,方法也没抛出RemoteException,HelloWorld接口地实现可以在很多环境下通过servlet容器来进行简单的测试,因为他和java远程接口没有关系,我们可可以仅仅使用JaxRpcHelloWorld类代表RemoteHelloWorld,但是将消弱对于RemoteHelloWorld接口的可重用性实现,因为所有的方法必须抛出RemoteExceptio和继Remote,使用HelloWorld接口,我们能够使他在某些环境中更简单的使用

服务端的spring配置文件applicationContext-server.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"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="helloWorldService" class="ch16.SimpleAxis.SimpleHelloWorld">
</bean>
</beans>

axis部署描述符server-config.wsdd 放在web-inf下

 

<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="ch16.SimpleAxis.JaxRpcHelloWorld"/>
       
<parameter name="allowedMethods" value="*"/>  
     
</service> 
     
     
<transport name="http">
       
<requestFlow>
          
<handler type="URLMapper"/>
       
</requestFlow>
     
</transport>
</deployment>

 

其中最重要的是service标签,他定义了WS的名字,有两个子标签,一个用来指定服务实现类的全名,一个用来定义服务中要被暴露的服务方法过滤器

指定java:RPC表明这是一个RPC风格的服务

web.xml

加入以下元素

 

<context-param>
      
<param-name>contextConfigLocation</param-name>
      
<param-value>/WEB-INF/applicationContext-server.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>    
    
    
<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>
    

 

部署这个web服务,我们在浏览器中运行如下地址(根据你自己的webapp)

http://localhost:81/ProSpringStudyWeb/services  可以看到你定义的web服务如下:

And now... Some Services

  • HelloWorld (wsdl)
    • getMessage

点击wsdl链接可以看到自动生成并保存的wdsl文档

 

客户端:

首先把服务端的两个HelloWorld接口打包成jar,加载到客户端classpath下,以便使用

使用JaxRpcPortProxyFactoryBean配置一个JAXRPC代理

applicationContext-client.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"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="helloWorldService" class="org.springframework.remoting.jaxrpc.JaxRpcPortProxyFactoryBean">
  
<property name="serviceFactoryClass">
    
<value>org.apache.axis.client.ServiceFactory</value>
  
</property>
  
<property name="wsdlDocumentUrl">
    
<value>http://localhost:81/ProSpringStudyWeb/services/HelloWorld?wsdl</value>
  
</property>
  
<property name="namespaceUri">
    
<value>http://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>

JaxRpcPortProxyFactoryBean有7个属性需要配置

ServiceFactoryClass ----  axis工厂类,在axis包中
wsdlDocumentUrl    ----   WS的wsdl地址,我们在测试服务端时可获得
namespaceURI      ----  可在wsdl中获得targetNamespace值
serviceName    ----   wsdl文件中<wsdl:service>标签中的name属性
portName   ---- wsdl文件中<wsdl:port>标签中的name属性
portInterface ----  远程接口
serviceInterface ---  POJO接口

ServiceInterface属性允许你指定你所成成代理所实现的接口,如果这是一个JAXRPC服务的远程接口,你可以不设置portInterface属性

客户端测试代码:

 

package ch16.SimpleAxis;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;



public class Test {

    
public static void main(String[] args) {
        ApplicationContext context 
= new ClassPathXmlApplicationContext("ch16/SimpleAxis/applicationContext-client.xml");
        HelloWorld helloWorld
=(HelloWorld)context.getBean("helloWorldService");
        System.out.println(helloWorld.getMessage());
    
    }


}

 

运行服务端,再运行客户端,经过日志后你会看到客户端输出了服务端的服务结果:hello world axis