XFier与Spring的整合

阅读更多

XFire 和 Spring 是可以完美整合的,上一篇写的有点糟糕,现在直接上更加清晰的例子吧!

 

XFire 的简单使用,我这里就不弄了,毕竟显示中很少使用,一般都是和框架结合使用如:spring 等; 

 

我在这里重新梳理下:

 

步骤:
1、 添加jar依赖
2、 修改web.xml
3、 修改applicationContext.xml
4、 编写接口和实现或校验类

1、在Maven 的 POM 文件中添加的 jar 包依赖

			
				javax.mail
				mail
				1.4
			
			
				org.codehaus.xfire
				xfire-aegis
				1.2.6
			
			
				org.codehaus.xfire
				xfire-spring
				1.2.6
				
	                
	                    spring
	                    org.springframework
	                
	            
			
			
				xalan
				xalan
				2.7.0
			

 

其中,要去掉XFier自带的spring1.2.6.jar 包,否则会包冲突的。

 

2、修改WEB.XML 文件


		contextConfigLocation
		
		classpath:org/codehaus/xfire/spring/xfire.xml,classpath:applicationContext.xml
	
	
		org.springframework.web.context.ContextLoaderListener
	
  
	
	 
	
		XFireServlet
		  
		org.codehaus.xfire.spring.XFireSpringServlet
		1
	

	
		XFireServlet
		/webservice/*
	
	
	

 

 注意,这里要引入 xfire-spring-1.2.6.jar 包中的 org/codehaus/xfire/spring/xfire.xml ,和配置 XFire的Mapping

 

3、修改applicationContext.xml

	
	
		
	
	
	
		
		
		
		
		
			
				
				
			
		
		
	
	
	
	

 

  这里是添加是。校验的 handlerMappingListener ,如果没有,可以去掉,不过,在生产环境下,校验还是必须要有点。是吧!

 

到现在为止。所有的配置就完成了。接下来。。我们只要编写我们的 接口 、实现类和 校验类了

接口:

 

 

 

package com.topinfo.xfire.webservice;

import com.topinfo.xfire.bean.SlNoticeBean;

/**
 *@Description: Web Service 接口
 *@Author:杨攀
 *@Since:2014年3月26日上午10:49:34
 */
public interface ITSMEStandardService {
	
	
	/**
	 *@Description: 同步新增通知公告
	 *@Author: 杨攀
	 *@Since: 2014年3月26日上午10:44:39
	 *@param slNoticeBean
	 *@return - 
	 */
	 public boolean synAddSlNotice(SlNoticeBean slNoticeBean);

	 
	 
	 /**
	 *@Description: 同步修改通知公告
	 *@Author: 杨攀
	 *@Since: 2014年3月26日上午10:44:39
	 *@param slNoticeBean
	 *@return -
	 */
	 public boolean synUpdateSlNotice(SlNoticeBean slNoticeBean);
	 
	 
	 
	/**
	 *@Description: 同步删除通知公告
	 *@Author: 杨攀
	 *@Since: 2014年3月26日上午10:47:26
	 *@param slNoticeBeanId
	 *@return
	 */
	public boolean synDeleteSlNotice(String slNoticeBeanId);
	
	
	
	
	
}

 

实现类:

package com.topinfo.xfire.webserviceImpl;

import org.apache.commons.lang3.StringUtils;

import com.topinfo.entity.SlNotice;
import com.topinfo.libs.BeanUtil;
import com.topinfo.service.SlNoticeService;
import com.topinfo.xfire.bean.SlNoticeBean;
import com.topinfo.xfire.webservice.ITSMEStandardService;

public class ITSMEStandardServiceImpl implements ITSMEStandardService {

	private SlNoticeService  slNoticeServiceImpl;
	
	 
	public SlNoticeService getSlNoticeServiceImpl() {
		return slNoticeServiceImpl;
	}

	public void setSlNoticeServiceImpl(SlNoticeService slNoticeServiceImpl) {
		this.slNoticeServiceImpl = slNoticeServiceImpl;
	}

	@Override
	public boolean synAddSlNotice(SlNoticeBean slNoticeBean) {
		boolean bool = false;
		if(slNoticeBean != null){
			try {
				SlNotice bean = new SlNotice();
				
				String [] fields = {"id","title","content","type","status","createTime","creator","updateTime","updator"};
				//复制 bean 的属性
				BeanUtil.copyProperties(slNoticeBean, bean, fields);
				String uuid = slNoticeServiceImpl.insert(bean);
				if(StringUtils.isNotBlank(uuid)){
					bool = true;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return bool;
	}

	@Override
	public boolean synUpdateSlNotice(SlNoticeBean slNoticeBean) {
		boolean bool = false;
		if(slNoticeBean != null){
			try {
				SlNotice bean = new SlNotice();
				
				String [] fields = {"id","title","content","type","status","createTime","creator","updateTime","updator"};
				//复制 bean 的属性
				BeanUtil.copyProperties(slNoticeBean, bean, fields);
				bool = slNoticeServiceImpl.update(bean);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return bool;
	}

	@Override
	public boolean synDeleteSlNotice(String slNoticeBeanId) {
		boolean bool = false;
		if(StringUtils.isNotBlank(slNoticeBeanId)){
			bool = slNoticeServiceImpl.delete(slNoticeBeanId);
		}
		return bool;
	}

}

 

校验监听类:

package com.topinfo.xfire.webservice.listener;

import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.handler.AbstractHandler;
import org.jdom.Element;

/**
 * @Description: 监听处理器
 * @Author:杨攀
 * @Since:2014年3月25日上午11:19:55
 */
public class HandlerMappingListener extends AbstractHandler {
 
	public void invoke(MessageContext context) throws Exception {
		// 为SOAP Header构造验证信息
		if (context.getInMessage().getHeader() == null) {
			throw new org.codehaus.xfire.fault.XFireFault("请求必须包含验证信息", org.codehaus.xfire.fault.XFireFault.SENDER);
		}

		Element token = context.getInMessage().getHeader().getChild("AuthenticationToken");
		if (token == null) {
			throw new org.codehaus.xfire.fault.XFireFault("请求必须包含身份验证信息", org.codehaus.xfire.fault.XFireFault.SENDER);
		}

		String username = token.getChild("Username").getValue();
		String password = token.getChild("Password").getValue();

		try {
			// 进行身份验证 ,只有admin/admin 的用户为授权用户
			if ("admin".equals(username) && "admin".equals(password)) {
				System.out.println("身份验证通过");
			} else {
				throw new Exception();
			}
		} catch (Exception e) {
			throw new org.codehaus.xfire.fault.XFireFault("非法的用户名和密码", org.codehaus.xfire.fault.XFireFault.SENDER);
		}
	}
}

 

实体:

package com.topinfo.xfire.bean;

import java.io.Serializable;

public class SlNoticeBean  implements Serializable {
	
	
	/** 
	 *@Fields serialVersionUID : 
	 */ 
	private static final long serialVersionUID = 6504083322529047064L;

	/**
     *主键编号 
     */ 
    private String id;
	
    /**
     *标题 
     */ 
    private String title;
	
    /**
     *内容 
     */ 
    private String content;
	
    /**
     *类型 
     */ 
    private String type;
	
    /**
     *发布状态 
     */ 
    private Long status;
	
    /**
     *发布时间 
     */ 
    private java.util.Date createTime;
	
    /**
     *发布人 
     */ 
    private String creator;
	
    /**
     *更新时间 
     */ 
    private java.util.Date updateTime;
	
    /**
     *更新人 
     */ 
    private String updator;
	
   //省略get set 方法
}

   注意, 在互联网上面传实体,请先序列化......这个不用多说的吧。

 

   这样, WebService 就完成了,

 

  接下来我们测试一下

 

  运行:访问:http://localhost:8080/ITSMEStandard/webservice/ITSMEStandardService?wsdl

 

  编写客户端:

 

    客户的代码可以有好几种实现

   1、 可以通过 wsdl 生产,网上有很多小工具

   2、 通过 eclipse 帮我们生产,需要引入插件,这个可以自己百度

   3、 自己写,哈哈哈

    必须把服务器的 接口 和实体 类打成 jar 包,提供给客户端,

   

package com.topinfo.test;

import java.lang.reflect.Proxy;
import java.net.MalformedURLException;

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 com.topinfo.xfire.bean.SlNoticeBean;
import com.topinfo.xfire.listener.ClientHandler;
import com.topinfo.xfire.webservice.ITSMEStandardService;
 
public class ITSTest {
	
	
	public static void main(String[] args) {
		 
		// 这里是创建一个service,需要传入一个接口类,因为我们后面必须调用相应的接口方法
		Service srcModel = new ObjectServiceFactory().create(ITSMEStandardService.class);

		// 代理工厂,这里是为了后面创建相应的接口类
		// XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
		XFireProxyFactory factory = new XFireProxyFactory();

		String readerServiceUrl = "http://localhost:8080/ITSMEStandard/webservice/ITSMEStandardService";

		try {
			// 利用工厂返回相应的接口类
			ITSMEStandardService service = (ITSMEStandardService) factory.create(srcModel,
					readerServiceUrl);
			
			SlNoticeBean slNoticeBean = new SlNoticeBean();
			slNoticeBean.setId("F5861A201A167A11E040A8C0E72829D6");
			slNoticeBean.setTitle("杨XXXXX网络体系.....");
			//boolean bool = service.synUpdateSlNotice(slNoticeBean);
			
			//在报头加入信息,供安全校验
			XFireProxy proxy = (XFireProxy) Proxy.getInvocationHandler(service); 
			Client client = proxy.getClient();
			// 发送授权信息  
            client.addOutHandler(new ClientHandler("admin","admin1")); 
			
			
			boolean bool = service.synDeleteSlNotice("F5861A201A167A11E040A8C0E72829D6");

			System.out.println(bool);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
	}

}

 

   

   OK  ,,到此结束!

 

 

你可能感兴趣的:(XFier,spring,校验,整合)