当前位置: 代码迷 >> Web前端 >> WebService与Spring组合
  详细解决方案

WebService与Spring组合

热度:139   发布时间:2012-11-11 10:07:57.0
WebService与Spring结合

?

环境:aixs1_4 ?spring 2.0.8

1.构建webservice环境

axis的需要的包有:

?

axis.jar
activation.jar
axis-ant.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
jaxrpc.jar
log4j-1.2.8.jar
mail.jar
saaj.jar
wsdl4j-1.5.1.jar
xalan.jar
xmlsec-1.4.3.jar

?在web.xml中添加:

?

<listener>
        <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>
    </listener>
    
  <servlet>
    <servlet-name>AxisServlet</servlet-name>
    <servlet-class>
        org.apache.axis.transport.http.AxisServlet
    </servlet-class>
  </servlet>

  <servlet>
    <servlet-name>AdminServlet</servlet-name>
    <servlet-class>
        org.apache.axis.transport.http.AdminServlet
    </servlet-class>
    <load-on-startup>100</load-on-startup>
  </servlet>

  <servlet>
    <servlet-name>SOAPMonitorService</servlet-name>
    <servlet-class>
        org.apache.axis.monitor.SOAPMonitorService
    </servlet-class>
    <init-param>
      <param-name>SOAPMonitorPort</param-name>
      <param-value>5001</param-value>
    </init-param>
    <load-on-startup>100</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/servlet/AxisServlet</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>*.jws</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>SOAPMonitorService</servlet-name>
    <url-pattern>/SOAPMonitor</url-pattern>
  </servlet-mapping>

 <!-- uncomment this if you want the admin servlet -->
 <!--
  <servlet-mapping>
    <servlet-name>AdminServlet</servlet-name>
    <url-pattern>/servlet/AdminServlet</url-pattern>
  </servlet-mapping>
 -->
	 <mime-mapping>
    <extension>wsdl</extension>
     <mime-type>text/xml</mime-type>
  </mime-mapping>
  

  <mime-mapping>
    <extension>xsd</extension>
    <mime-type>text/xml</mime-type>
  </mime-mapping>

?2.实现提供服务器端功能的接口与类:(这里需要一个接口,在与spring结合时会用到,这里会提供一些方法供client调用)

在这里有一点要注意,返回的结果最好不要是List,大家可以看到下面的接口所有的方法返回值都是数组,大家可以试下两种方式的区别,当返回list时,生成人wsdl文件中不会有自定义定型的详细描述,如这里的HttpHistoryRecord,如果返回list的话,wsdl中只知道返回的数组中是一个Object,不会有任何它的描述信息,而返回数组时,

HttpHistoryRecord中所有的字段信息都会在wsdl中出现,client根据这些能生成对应的本地类

public interface OperatorHistoryRecord
{
	/**
	 * 
	 * 功能简述:根据IP获取对应时间范围内的http测试结果信息
	 * 功能详细描述:
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param ip	用户IP
	 * @return
	 */
	public HttpHistoryRecord[] findHttpHistoryByUrl(String startTime,String endTime,String ip,String httpUrl);

	/**
	 * 
	 * 功能简述:根据IP获取对应时间范围内的http针对某个url的测试结果信息
	 * 功能详细描述:
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param ip	用户IP
	 * @return
	 */
	public HttpHistoryRecord[] findHttpHistory(String startTime,String endTime,String ip);
	
	/**
	 * 
	 * 功能简述:根据IP获取对应时间范围内的ICMP测试结果信息
	 * 功能详细描述:
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param ip	用户IP
	 * @return
	 */
	public IcmpHistoryRecord[] findIcmpHistory(String startTime,String endTime,String ip);
	
	/**
	 * 
	 * 功能简述:根据IP获取对应时间范围内的tcp测试结果信息
	 * 功能详细描述:
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param ip	用户IP
	 * @return
	 */
	public TcpHistoryRecord[] findTcpHistory(String startTime,String endTime,String ip);
	
	/**
	 * 
	 * 功能简述:根据IP获取对应时间范围内的tcp上行测试结果信息
	 * 功能详细描述:
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param ip	用户IP
	 * @return
	 */
	public TcpHistoryRecord[] findTcpUpHistory(String startTime,String endTime,String ip);
	
	/**
	 * 
	 * 功能简述:根据IP获取对应时间范围内的tcp下行测试结果信息
	 * 功能详细描述:
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param ip	用户IP
	 * @return
	 */
	public TcpHistoryRecord[] findTcpDownHistory(String startTime,String endTime,String ip);
	
	/**
	 * 
	 * 功能简述:根据IP获取对应时间范围内的udp测试结果信息
	 * 功能详细描述:
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param ip	用户IP
	 * @return
	 */
	public UdpHistoryRecord[] findUdpHistory(String startTime,String endTime,String ip);
	
	/**
	 * 
	 * 功能简述:根据IP获取对应时间范围内的udp上行测试结果信息
	 * 功能详细描述:
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param ip	用户IP
	 * @return
	 */
	public UdpHistoryRecord[] findUdpUpHistory(String startTime,String endTime,String ip);
	
	/**
	 * 
	 * 功能简述:根据IP获取对应时间范围内的udp下行测试结果信息
	 * 功能详细描述:
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param ip	用户IP
	 * @return
	 */
	public UdpHistoryRecord[] findUdpDownHistory(String startTime,String endTime,String ip);
	
}

?

public class OperatorHistoryRecordImpl implements OperatorHistoryRecord
{
	
	private TestingHistoryService testingHistoryService;
	
	
	
	public void setTestingHistoryService(TestingHistoryService testingHistoryService)
	{
		this.testingHistoryService = testingHistoryService;
	}

	/**
	 * 
	 * 功能简述:根据IP获取对应时间范围内的http测试结果信息
	 * 功能详细描述:
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param ip	用户IP
	 * @return
	 */
	public HttpHistoryRecord[] findHttpHistoryByUrl(String startTime,String endTime,String ip,String httpUrl){
		List<HttpHistoryRecord> httpHistorys = testingHistoryService.getHttpHistoryByUrl(startTime, endTime, ip, httpUrl);
		return httpHistorys.toArray(new HttpHistoryRecord[0]);
	}

	/**
	 * 
	 * 功能简述:根据IP获取对应时间范围内的http针对某个url的测试结果信息
	 * 功能详细描述:
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param ip	用户IP
	 * @return
	 */
	public HttpHistoryRecord[] findHttpHistory(String startTime,String endTime,String ip){
		List<HttpHistoryRecord> httpHistorys = testingHistoryService.getHttpHistory(startTime, endTime, ip);
		return httpHistorys.toArray(new HttpHistoryRecord[0]);
	}
	
	/**
	 * 
	 * 功能简述:根据IP获取对应时间范围内的ICMP测试结果信息
	 * 功能详细描述:
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param ip	用户IP
	 * @return
	 */
	public IcmpHistoryRecord[] findIcmpHistory(String startTime,String endTime,String ip){
		List<IcmpHistoryRecord> icmpHistorys = testingHistoryService.getIcmpHistory(startTime, endTime, ip);
		return icmpHistorys.toArray(new IcmpHistoryRecord[0]);
	}
	
	/**
	 * 
	 * 功能简述:根据IP获取对应时间范围内的tcp测试结果信息
	 * 功能详细描述:
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param ip	用户IP
	 * @return
	 */
	public TcpHistoryRecord[] findTcpHistory(String startTime,String endTime,String ip){
		List<TcpHistoryRecord> tcpHistorys = testingHistoryService.getTcpHistory(startTime, endTime, ip);
		return tcpHistorys.toArray(new TcpHistoryRecord[0]);
	}
	
	/**
	 * 
	 * 功能简述:根据IP获取对应时间范围内的tcp上行测试结果信息
	 * 功能详细描述:
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param ip	用户IP
	 * @return
	 */
	public TcpHistoryRecord[] findTcpUpHistory(String startTime,String endTime,String ip){
		List<TcpHistoryRecord> tcpHistorys = testingHistoryService.getTcpUpHistory(startTime, endTime, ip);
		return tcpHistorys.toArray(new TcpHistoryRecord[0]);
	}
	
	/**
	 * 
	 * 功能简述:根据IP获取对应时间范围内的tcp下行测试结果信息
	 * 功能详细描述:
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param ip	用户IP
	 * @return
	 */
	public TcpHistoryRecord[] findTcpDownHistory(String startTime,String endTime,String ip){
		List<TcpHistoryRecord> tcpHistorys = testingHistoryService.getTcpDownHistory(startTime, endTime, ip);
		return tcpHistorys.toArray(new TcpHistoryRecord[0]);
	}
	
	/**
	 * 
	 * 功能简述:根据IP获取对应时间范围内的udp测试结果信息
	 * 功能详细描述:
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param ip	用户IP
	 * @return
	 */
	public UdpHistoryRecord[] findUdpHistory(String startTime,String endTime,String ip){
		List<UdpHistoryRecord> udpHistorys = testingHistoryService.getUdpHistory(startTime, endTime, ip);
		return udpHistorys.toArray(new UdpHistoryRecord[0]);
	}
	
	/**
	 * 
	 * 功能简述:根据IP获取对应时间范围内的udp上行测试结果信息
	 * 功能详细描述:
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param ip	用户IP
	 * @return
	 */
	public UdpHistoryRecord[] findUdpUpHistory(String startTime,String endTime,String ip){
		List<UdpHistoryRecord> udpHistorys = testingHistoryService.getUdpUpHistory(startTime, endTime, ip);
		return udpHistorys.toArray(new UdpHistoryRecord[0]);
	}
	
	/**
	 * 
	 * 功能简述:根据IP获取对应时间范围内的udp下行测试结果信息
	 * 功能详细描述:
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param ip	用户IP
	 * @return
	 */
	public UdpHistoryRecord[] findUdpDownHistory(String startTime,String endTime,String ip){
		List<UdpHistoryRecord> udpHistorys = testingHistoryService.getUdpDownHistory(startTime, endTime, ip);
		return udpHistorys.toArray(new UdpHistoryRecord[0]);
	}
	
}

?3.与spring结合(建一个类继承ServletEndpointSupport,当client请求webservice时,它会来管理相应的实现类,接着继承OperatorHistoryRecord)

?

/**
 * 功能简述:使用spring来管理soap对象
 * 功能详细描述:
 * @author 曾阁
 * @version [1.0,2011-3-18 上午10:31:40]
 */
public class SpringOperatorHistoryRecord extends ServletEndpointSupport implements OperatorHistoryRecord
{

	private OperatorHistoryRecord operatorHistoryRecord ;
	
	@Override
	protected void onInit() throws ServiceException
	{
		operatorHistoryRecord = (OperatorHistoryRecord)getApplicationContext().getBean("operatorHistoryRecord");
		super.onInit();
	}

	public IcmpHistoryRecord[] findIcmpHistory(String startTime,
			String endTime, String ip)
	{
		return operatorHistoryRecord.findIcmpHistory(startTime, endTime, ip);
	}

	public TcpHistoryRecord[] findTcpHistory(String startTime, String endTime,
			String ip)
	{
		return operatorHistoryRecord.findTcpHistory(startTime, endTime, ip);
	}

	public HttpHistoryRecord[] findHttpHistoryByUrl(String startTime,
			String endTime, String ip, String httpUrl)
	{
		return operatorHistoryRecord.findHttpHistoryByUrl(startTime, endTime, ip, httpUrl);
	}

	public UdpHistoryRecord[] findUdpHistory(String startTime, String endTime,
			String ip)
	{
		return operatorHistoryRecord.findUdpHistory(startTime, endTime, ip);
	}

	public TcpHistoryRecord[] findTcpDownHistory(String startTime,
			String endTime, String ip)
	{
		return operatorHistoryRecord.findTcpDownHistory(startTime, endTime, ip);
	}

	public TcpHistoryRecord[] findTcpUpHistory(String startTime,
			String endTime, String ip)
	{
		return operatorHistoryRecord.findTcpUpHistory(startTime, endTime, ip);
	}

	public UdpHistoryRecord[] findUdpDownHistory(String startTime,
			String endTime, String ip)
	{
		return operatorHistoryRecord.findUdpDownHistory(startTime, endTime, ip);
	}

	public UdpHistoryRecord[] findUdpUpHistory(String startTime,
			String endTime, String ip)
	{
		return operatorHistoryRecord.findUdpUpHistory(startTime, endTime, ip);
	}

	public HttpHistoryRecord[] findHttpHistory(String startTime,
			String endTime, String ip)
	{
		return operatorHistoryRecord.findHttpHistory(startTime, endTime, ip);
	}

}

?spring配置文件:

?

<bean id="operatorHistoryRecord" class="com.vixtel.soap.server.OperatorHistoryRecordImpl"></bean>
<bean id="testingHistoryService" class="com.vixtel.netmanager.service.impl.TestingHistoryServiceImpl"></bean>

?operatorHistoryRecord为提供webservice功能的对象,它会用到testingHistoryService

4.生成webservice布署描述文件(deploy.wsdd)

在WEB-INF下面添加deploy.wsdd

?

<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
  <service name="OperatorHistoryRecord" provider="java:RPC">
 	<parameter name="className" value="com.vixtel.soap.server.SpringOperatorHistoryRecord"/>
	<parameter name="allowedMethods" value="*" />
	<beanMapping languageSpecificType="java:com.vixtel.soap.server.vo.HttpHistoryRecord" qname="myNS:HttpHistoryRecord" xmlns:myNS="urn:OperatorHistoryRecord"/>
	<beanMapping languageSpecificType="java:com.vixtel.soap.server.vo.IcmpHistoryRecord" qname="myNS:IcmpHistoryRecord" xmlns:myNS="urn:OperatorHistoryRecord"/>
	<beanMapping languageSpecificType="java:com.vixtel.soap.server.vo.TcpHistoryRecord" qname="myNS:TcpHistoryRecord" xmlns:myNS="urn:OperatorHistoryRecord"/>
	<beanMapping languageSpecificType="java:com.vixtel.soap.server.vo.UdpHistoryRecord" qname="myNS:UdpHistoryRecord" xmlns:myNS="urn:OperatorHistoryRecord"/>
	<parameter name="scope" value="request" />
 </service>
</deployment>

?这个文件用于描述提供的服务,如果没与spring结合的话

?

<parameter name="className" value="com.vixtel.soap.server.SpringOperatorHistoryRecord"/>
应该换成
<parameter name="className" value="com.vixtel.soap.server.OperatorHistoryRecordImpl"/>

?<parameter name="allowedMethods" value="*" />表示SpringOperatorHistoryRecord中所有的方法都用于提供服务,当然继承来的不算

?

<beanMapping languageSpecificType="java:com.vixtel.soap.server.vo.HttpHistoryRecord" qname="myNS:HttpHistoryRecord" xmlns:myNS="urn:OperatorHistoryRecord"/>
用于描述自定义的类型,java:com.vixtel.soap.server.vo.HttpHistoryRecord表示类型,前面的java:不能少,后面的表示命名空间等等,不用太在意

?

此文件其实可以完全不要,因为最终它内部的的service内容会被添加到server-config.wsdd里面去,这才是最终用到的wsdd。一般会调用命令首先启动tomcat,进入WEB-INF目录下执行命令:

java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient deploy.wsdd

这句命令执行后会在WEB-INF下面生成一个server-config.wsdd,并且会将deploy.wsdd的内容添加到server-config.wsdd之中。所以我们可以手动的将axis中的server-config.wsdd copy一份到WEB-INF下面,然后将deploy.wsdd中的内容添加到server-config.wsdd之中

?

server-config.wsdd

?

<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
 <globalConfiguration>
  <parameter name="sendMultiRefs" value="true"/>
  <parameter name="disablePrettyXML" value="true"/>
  <parameter name="adminPassword" value="admin"/>
  <parameter name="attachments.Directory" value="E:\zg\softInstall\Tomcat\webapps\axis\WEB-INF\attachments"/>
  <parameter name="dotNetSoapEncFix" value="true"/>
  <parameter name="enableNamespacePrefixOptimization" value="false"/>
  <parameter name="sendXMLDeclaration" value="true"/>
  <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>
  <parameter name="sendXsiTypes" value="true"/>
  <requestFlow>
   <handler type="java:org.apache.axis.handlers.JWSHandler">
    <parameter name="scope" value="session"/>
   </handler>
   <handler type="java:org.apache.axis.handlers.JWSHandler">
    <parameter name="scope" value="request"/>
    <parameter name="extension" value=".jwr"/>
   </handler>
  </requestFlow>
 </globalConfiguration>
 <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
 <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
 <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
 <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>
   <!--只有这一段是所需要的,这里只是将原始的server-config.wsdd复制过来,然后将所需要的内容粘在这了,所以deploy.wsdd是可以不要的,并且也不用执行什么命令了-->
 <service name="OperatorHistoryRecord" provider="java:RPC">
 	<parameter name="className" value="com.vixtel.soap.server.SpringOperatorHistoryRecord"/>
	<parameter name="allowedMethods" value="*" />
	<beanMapping languageSpecificType="java:com.vixtel.soap.server.vo.HttpHistoryRecord" qname="myNS:HttpHistoryRecord" xmlns:myNS="urn:OperatorHistoryRecord"/>
	<beanMapping languageSpecificType="java:com.vixtel.soap.server.vo.IcmpHistoryRecord" qname="myNS:IcmpHistoryRecord" xmlns:myNS="urn:OperatorHistoryRecord"/>
	<beanMapping languageSpecificType="java:com.vixtel.soap.server.vo.TcpHistoryRecord" qname="myNS:TcpHistoryRecord" xmlns:myNS="urn:OperatorHistoryRecord"/>
	<beanMapping languageSpecificType="java:com.vixtel.soap.server.vo.UdpHistoryRecord" qname="myNS:UdpHistoryRecord" xmlns:myNS="urn:OperatorHistoryRecord"/>
	<parameter name="scope" value="request" />
 </service>
  
 <transport name="http">
  <requestFlow>
   <handler type="URLMapper"/>
   <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
  </requestFlow>
  <parameter name="qs:list" value="org.apache.axis.transport.http.QSListHandler"/>
  <parameter name="qs:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
  <parameter name="qs.list" value="org.apache.axis.transport.http.QSListHandler"/>
  <parameter name="qs.method" value="org.apache.axis.transport.http.QSMethodHandler"/>
  <parameter name="qs:method" value="org.apache.axis.transport.http.QSMethodHandler"/>
  <parameter name="qs.wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
 </transport>
 <transport name="local">
  <responseFlow>
   <handler type="LocalResponder"/>
  </responseFlow>
 </transport>
</deployment>

?

这样一个webservice就建好了,并与spring结合好了,启动tomcat,进入http://localhost:8080/project_name/services就能看到相应的webservice的名称,点击链接进去就能看到对应的wsdl文件,用它来生成client调用的对象

?

?

?

?

?

?

?

?

?

?

?

?

?

  相关解决方案