Xfire Web Services + Spring2.5的整合

Xfire Web Services + Spring2.5的整合

1.需要的jar包括:xfire的核心jar包,Spring的jar包(Spring-webmvc.jar必须需要)。

2.以例子作介绍:

3.向Spring的项目工程中加入xfire webService的jar包

Web.xml文件的配置:

<!-- begin XFire 配置 -->
 <servlet>
  <servlet-name>xfire</servlet-name>
  <servlet-class>
   org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>xfire</servlet-name>
  <url-pattern>*.ws</url-pattern>
 </servlet-mapping>
 <servlet>
  <!-- 配合Spring容器中XFire一起工作的Servlet-->
  <servlet-name>xfireServlet</servlet-name>
  <servlet-class>
   org.codehaus.xfire.spring.XFireSpringServlet
  </servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>xfireServlet</servlet-name>
  <!-- 在这个URI下开放Web Service服务 -->
  <url-pattern>/service/*</url-pattern>
 </servlet-mapping>
 <!-- end XFire 配置 -->

创建xfire-servlet.xml文件:根据自己工程创建的文件将其加载web.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:aop="
http://www.springframework.org/schema/aop"
 xmlns:tx="
http://www.springframework.org/schema/tx"
 xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

 <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
 <!-- 定义访问的url -->
 <bean
  class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="urlMap">
   <map>
    <entry key="/webservices.ws">
     <ref bean="webservices" />
    </entry>
   </map>
  </property>
 </bean>

 <bean id="orderWebServiceImpl"
  class="com.centfor.zmd.xfire.impl.OrderWebServiceImpl">
  <property name="cfCsOrderDetailService"
   ref="cfCsOrderDetailService" />
 </bean><!--依然还是很简单。-->

 <!-- 使用XFire导出器 -->
 <bean id="baseWebService"
  class="org.codehaus.xfire.spring.remoting.XFireExporter"
  lazy-init="false" abstract="true">
  <!-- 引用xfire.xml中定义的工厂 -->
  <property name="serviceFactory" ref="xfire.serviceFactory" />
  <!-- 引用xfire.xml中的xfire实例 -->
  <property name="xfire" ref="xfire" />
 </bean>

 <bean id="webservices" parent="baseWebService">
  <!-- 业务服务bean -->
  <property name="serviceBean" ref="orderWebServiceImpl" />
  <!-- 业务服务bean的窄接口类 -->
  <property name="serviceClass"
   value="com.centfor.zmd.xfire.IOrderWebService">
  </property>
 </bean>


</beans>

 

4.一下是实现类

Java接口类:

package com.centfor.zmd.xfire;


public interface IOrderWebService {
 public String aa(String a);

 public boolean OrderSave(String merchcode, String orderid,
   String ordercount, String price);
}

 

Java接口实现类:

package com.centfor.zmd.xfire.impl;

import org.apache.commons.lang.math.NumberUtils;

import com.centfor.zmd.business.dto.bi.cigarette.CfBiCigaretteDto;
import com.centfor.zmd.business.dto.cs.order.CfCsOrderDetailDto;
import com.centfor.zmd.business.dto.cs.order.CfCsOrderMainDto;
import com.centfor.zmd.business.services.cs.order.ICfCsOrderDetailService;
import com.centfor.zmd.web.core.BaseCrudActionSupport;
import com.centfor.zmd.xfire.IOrderWebService;

/**
 *
 * 类说明
 * 
 */
public class OrderWebServiceImpl extends
  BaseCrudActionSupport<CfCsOrderDetailDto> implements IOrderWebService {

 /**
  *
  */
 private static final long serialVersionUID = 1L;
 private ICfCsOrderDetailService cfCsOrderDetailService;

 public void setCfCsOrderDetailService(
   ICfCsOrderDetailService cfCsOrderDetailService) {
  this.cfCsOrderDetailService = cfCsOrderDetailService;
  this.baseService = cfCsOrderDetailService;
 }

 @Override
 public boolean OrderSave(String merchcode, String orderid,
   String ordercount, String price) {

  CfCsOrderDetailDto cfCsOrderDetailDto = new CfCsOrderDetailDto();
  CfBiCigaretteDto cfBiCigaretteDto = new CfBiCigaretteDto();
  cfBiCigaretteDto.setMerchcode(merchcode);
  CfCsOrderMainDto cfCsOrderMainDto = new CfCsOrderMainDto();
  cfCsOrderMainDto.setOrderid(orderid);
  // cfCsOrderDetailDto.setId("31");
  cfCsOrderDetailDto.setCfBiCigaretteDto(cfBiCigaretteDto);
  cfCsOrderDetailDto.setCfCsOrderMain(cfCsOrderMainDto);
  cfCsOrderDetailDto.setOrdercount(NumberUtils.toInt(ordercount));
  cfCsOrderDetailDto.setPrice(NumberUtils.toDouble(price));

  boolean b = false;
  b = cfCsOrderDetailService.save(cfCsOrderDetailDto);
  return b;
 }

 @Override
 public String aa(String a) {
  if (a != null) {
   return "yes";
  }
  return "no";
 }
}

输入http://localhost:8080/项目名/service/IOrderWebServices?wsdl;将得到的文件保存为.wsdl格式的文档,并将其放到lib目录下。运行下面的测试类:

Java接口测试类:

package com.centfor.zmd.xfire;

import java.lang.reflect.Proxy;

import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxy;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.codehaus.xfire.transport.http.CommonsHttpMessageSender;

import com.centfor.zmd.business.dto.bi.cigarette.CfBiCigaretteDto;
import com.centfor.zmd.business.dto.cs.order.CfCsOrderDetailDto;
import com.centfor.zmd.business.dto.cs.order.CfCsOrderMainDto;

/**
 *
 * 类说明
 * 
 */
public class RemoteXfire {
 public boolean OrderSave() {
  boolean b = false;
  String serviceURL = "
http://localhost:8080/zmd/service/IOrderWebService"; // webservice测试地址
  System.out.println("serviceURL=" + serviceURL);
  // 创建service对象
  Service serviceModel = new ObjectServiceFactory().create(
    IOrderWebService.class, "webservices",
    "
http://10.0.111.137:8080/zmd/service", null);
  XFireProxyFactory serviceFactory = new XFireProxyFactory();

  try {
   // 获取服务对象
   IOrderWebService service = (IOrderWebService) serviceFactory
     .create(serviceModel, serviceURL);
   // 获取客户端代理
   Client client = ((XFireProxy) Proxy.getInvocationHandler(service))
     .getClient();
   // 同时启动response和request压缩
   client.setProperty(CommonsHttpMessageSender.GZIP_ENABLED,
     Boolean.FALSE);
   // 忽略超时
   client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "0");

   CfCsOrderDetailDto cfCsOrderDetailDto = new CfCsOrderDetailDto();

   CfBiCigaretteDto cfBiCigaretteDto = new CfBiCigaretteDto();
   cfBiCigaretteDto.setMerchcode("31010501");
   CfCsOrderMainDto cfCsOrderMainDto = new CfCsOrderMainDto();
   cfCsOrderMainDto.setOrderid("20090407013");
   cfCsOrderDetailDto.setCfCsOrderMain(cfCsOrderMainDto);
   cfCsOrderDetailDto.setCfBiCigaretteDto(cfBiCigaretteDto);
   // b = service.OrderSave(cfCsOrderDetailDto);
   System.out.println(b);
  } catch (Exception e) {
   System.out.println(e);
   e.printStackTrace();
  }
  return b;
 }

 /**
  * 测试接口
  *
  * @param args
  */
 public static void main(String[] args) {
  RemoteXfire obj = new RemoteXfire();
  obj.OrderSave();
 }

}

你可能感兴趣的:(web services)