`
zqding
  • 浏览: 96073 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

利用AXIS开发Webservice综合应用

阅读更多

利用AXIS开发WebService综合应用:

因项目需要,通过WebService传递对象及附件,最终返回结果对象。

1、开发服务端参数对象:

派单对象:

 

package com.starit.itsmhd.bean;

import java.io.Serializable;
import java.util.Date;

/**
 * @author zqding
 * @version 1.0
 * @date 2010-11-19
 * @class_displayName ApplyItem *
 */

public class ApplyItem implements Serializable {
	private static final long serialVersionUID = -4997055778736225953L;

	// 事件流水号
	private String applyserial;

	// 事件提出人
	private String submitStaffName;

	// 事件标题
	private String title;

	// 地点
	private String place;

	// 事件发生时间
	private Date occurTime;

	// 事件来源
	private int origin;

	// 事件影响度
	private int incidence;

	// 事件优先级
	private int priority;

	// 事件完成期限
	private Date finishLimit;

	// 所属系统类型
	private int sysType;

	// 事件内容
	private String content;

	// 附件标记
	private boolean attachFlag;

	// 附件数量
	private int fileNum;

	// 附件名称
	private String[] attName;

	public ApplyItem() {

	}

	public String getApplyserial() {
		return applyserial;
	}

	public void setApplyserial(String applyserial) {
		this.applyserial = applyserial;
	}

	public boolean isAttachFlag() {
		return attachFlag;
	}

	public void setAttachFlag(boolean attachFlag) {
		this.attachFlag = attachFlag;
	}

	public String[] getAttName() {
		return attName;
	}

	public void setAttName(String[] attName) {
		this.attName = attName;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public int getFileNum() {
		return fileNum;
	}

	public void setFileNum(int fileNum) {
		this.fileNum = fileNum;
	}

	public Date getFinishLimit() {
		return finishLimit;
	}

	public void setFinishLimit(Date finishLimit) {
		this.finishLimit = finishLimit;
	}

	public int getIncidence() {
		return incidence;
	}

	public void setIncidence(int incidence) {
		this.incidence = incidence;
	}

	public Date getOccurTime() {
		return occurTime;
	}

	public void setOccurTime(Date occurTime) {
		this.occurTime = occurTime;
	}

	public int getOrigin() {
		return origin;
	}

	public void setOrigin(int origin) {
		this.origin = origin;
	}

	public String getPlace() {
		return place;
	}

	public void setPlace(String place) {
		this.place = place;
	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public String getSubmitStaffName() {
		return submitStaffName;
	}

	public void setSubmitStaffName(String submitStaffName) {
		this.submitStaffName = submitStaffName;
	}

	public int getSysType() {
		return sysType;
	}

	public void setSysType(int sysType) {
		this.sysType = sysType;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

}

 

 派单返回对象:

 

package com.starit.itsmhd.bean;

import java.io.Serializable;

/**
 * @author zqding
 * @version 1.0
 * @date 2010-11-21
 * @class_displayName ApplyItemResponse
 */

public class ApplyItemResponse implements Serializable {

	private static final long serialVersionUID = -443142827992526070L;

	// 返回结果
	private int result;

	// 服务保障流程号
	private String fwbzSerial;

	// ITSM流程号
	private String itsmSerial;

	// 返回信息
	private String msg;

	public ApplyItemResponse() {
		
	}

	public String getFwbzSerial() {
		return fwbzSerial;
	}

	public void setFwbzSerial(String fwbzSerial) {
		this.fwbzSerial = fwbzSerial;
	}

	public String getItsmSerial() {
		return itsmSerial;
	}

	public void setItsmSerial(String itsmSerial) {
		this.itsmSerial = itsmSerial;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}

}
 

 

其它对象略...

 

2、配置wsdd文件:

 

<service name="FwbzService" provider="java:RPC" style="rpc" use="encoded">
	<documentation>接口</documentation>
	<parameter name="allowedMethods" value="*"/>
	<parameter name="wsdlPortType" value="FwbzService"/>
    <parameter name="wsdlServicePort" value="FwbzService"/>
	<parameter name="wsdlServiceElement" value="FwbzService"/>
	<parameter name="className" value="com.starit.itsmhd.service.FwbzService"/>
	<parameter name="wsdlTargetNamespace" value="http://itsmhd.starit.com/service"/>
    <parameter name="wsdlServiceElement" value="FwbzService"/>
    <parameter name="schemaUnqualified" value="http://itsmhd.starit.com/bean,http://itsmhd.starit.com/exception"/>
	<!--派发-->
    <typeMapping
		xmlns:ns="http://itsmhd.starit.com/bean"
		qname="ns:ApplyItem"
		type="java:com.starit.itsmhd.bean.ApplyItem"
		serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
		deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
		encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
    />
	<typeMapping
		xmlns:ns="http://itsmhd.starit.com/bean"
		qname="ns:ApplyItemResponse"
		type="java:com.starit.itsmhd.bean.ApplyItemResponse"
		serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
		deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
		encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
	/>
    <!--查询--> 
	<typeMapping
		xmlns:ns="http://itsmhd.starit.com/bean"
		qname="ns:QueryItem"
		type="java:com.starit.itsmhd.bean.QueryItem"
		serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
		deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
		encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
	/>
	<typeMapping
		xmlns:ns="http://itsmhd.starit.com/bean"
		qname="ns:QueryItemResponse"
		type="java:com.starit.itsmhd.bean.QueryItemResponse"
		serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
		deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
		encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
	/>	
	<!-- 取消--> 
	 <typeMapping
		xmlns:ns="http://itsmhd.starit.com/bean"
		qname="ns:CancelItem"
		type="java:com.starit.itsmhd.bean.CancelItem"
		serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
		deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
		encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
	/>
	<typeMapping
		xmlns:ns="http://itsmhd.starit.com/bean"
		qname="ns:CancelItemResponse"
		type="java:com.starit.itsmhd.bean.CancelItemResponse"
		serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
		deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
		encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
	/>
    <!-- 关闭-->
	<typeMapping
		xmlns:ns="http://itsmhd.starit.com/bean"
		qname="ns:CloseItem"
		type="java:com.starit.itsmhd.bean.CloseItem"
		serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
		deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
		encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
	/>
    <typeMapping
		xmlns:ns="http://itsmhd.starit.com/bean"
		qname="ns:CloseItemResponse"
		type="java:com.starit.itsmhd.bean.CloseItemResponse"
		serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
		deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
		encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
	/>
	<!--催单-->
	<typeMapping
		xmlns:ns="http://itsmhd.starit.com/bean"
		qname="ns:PromptItem"
		type="java:com.starit.itsmhd.bean.PromptItem"
		serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
		deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
		encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
    />
	<typeMapping
		xmlns:ns="http://itsmhd.starit.com/bean"
		qname="ns:PromptItemResponse"
		type="java:com.starit.itsmhd.bean.PromptItemResponse"
		serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
		deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
		encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
    />
	<!--附件-->
	<arrayMapping
		xmlns:ns="http://itsmhd.starit.com/bean"
		qname="ns:ArrayOf_tns1_DataHandler"
		type="java:javax.activation.DataHandler[]"
		innerType="cmp-ns:DataHandler" 
		xmlns:cmp-ns="http://xml.apache.org/xml-soap"
		encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
    />
	<!--异常-->
    <typeMapping
		xmlns:ns="http://itsmhd.starit.com/exception"
		qname="ns:AppException"
		type="java:com.starit.itsmhd.exception.AppException"
		serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
		deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
		encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
    />
 </service>

 

 

3、开发功能接口及实现和业务处理类:

 

package com.starit.itsmhd.interfaces;

import java.rmi.Remote;
import java.rmi.RemoteException;

import javax.activation.DataHandler;

import com.starit.itsmhd.bean.ApplyItem;
import com.starit.itsmhd.bean.ApplyItemResponse;
import com.starit.itsmhd.bean.CancelItem;
import com.starit.itsmhd.bean.CancelItemResponse;
import com.starit.itsmhd.bean.CloseItem;
import com.starit.itsmhd.bean.CloseItemResponse;
import com.starit.itsmhd.bean.PromptItem;
import com.starit.itsmhd.bean.PromptItemResponse;
import com.starit.itsmhd.bean.QueryItem;
import com.starit.itsmhd.bean.QueryItemResponse;
import com.starit.itsmhd.exception.AppException;

/**
 * @author zqding
 * @version 1.0
 * @date 2010-11-22
 * @class_displayName FwbzInter
 */

public interface FwbzInter extends Remote {
	public ApplyItemResponse putEvent(ApplyItem applyItem,
			DataHandler[] attchments) throws RemoteException, AppException;

	public QueryItemResponse queryEvent(QueryItem queryItem)
			throws RemoteException, AppException;

	public CancelItemResponse cancelEvent(CancelItem cancelItem)
			throws RemoteException, AppException;

	public CloseItemResponse closeEvent(CloseItem closeItem,
			DataHandler[] attchments) throws RemoteException, AppException;

	public PromptItemResponse promptEvent(PromptItem promptItem)
			throws RemoteException, AppException;

}

 

 

 

package com.starit.itsmhd.impl;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;

import javax.activation.DataHandler;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.primeton.tp.common.xml.XmlUtil;
import com.primeton.tp.core.api.BizServiceCallerFactory;
import com.primeton.tp.core.bizservice.ProcessCaller;
import com.starit.itsmhd.bean.ApplyItem;
import com.starit.itsmhd.bean.ApplyItemResponse;
import com.starit.itsmhd.bean.CancelItem;
import com.starit.itsmhd.bean.CancelItemResponse;
import com.starit.itsmhd.bean.CloseItem;
import com.starit.itsmhd.bean.CloseItemResponse;
import com.starit.itsmhd.bean.PromptItem;
import com.starit.itsmhd.bean.PromptItemResponse;
import com.starit.itsmhd.bean.QueryItem;
import com.starit.itsmhd.bean.QueryItemResponse;
import com.starit.itsmhd.exception.AppException;
import com.starit.itsmhd.interfaces.FwbzInter;

/**
 * @author zqding
 * @version 1.0
 * @date 2010-11-22
 * @class_displayName FwbzImpl
 */

public class FwbzImpl implements FwbzInter {

	private static Log logger = LogFactory.getLog(FwbzImpl.class);

	public ApplyItemResponse putEvent(ApplyItem applyItem,	DataHandler[] attchments) throws RemoteException, AppException {
		
		ApplyItemResponse resultObj = new ApplyItemResponse();
		//ApplyItemResponse resultObj = null;
		String resultValue = null;
		String resultXML = "";
		try {
			logger.info("开始处理 ITSM系统的工单派发请求接口<ITSM调服务保障>");
			
			//1、如果有附件,先保存附件。
			logger.info("保存附件");
			String[] fileNames = applyItem.getAttName();
			String[] fNames = {"222.pdf","222.rar"};
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
			String inputXML = "";
			
			if(applyItem.isAttachFlag()){
				for(int i =0 ;i < fileNames.length ; i++){
					logger.info("保存文件" + i +":"+ fileNames[i]);
					String fullPath = "C:/" + fNames[i];   
			        File file = new File(fullPath);   
			        int flag = 0;   
			        try {   
			            InputStream input = attchments[i].getInputStream();   
			            FileOutputStream fos = new FileOutputStream(file);   
			            byte[] buffer=new byte[4096];   
			            while((flag=input.read(buffer))!=-1){   
			                fos.write(buffer,0,flag);   
			            }   
			            input.close();   
			            fos.close();   
			        } catch (IOException e) {   
			            e.printStackTrace();   
			        }   
			    }
			}
			
			//2、向保障系统派单
			logger.info("参数列表:");
			logger.info("ITSM事件流水号:" + applyItem.getApplyserial());
			logger.info("事件提出人:" 	  + applyItem.getSubmitStaffName());
			logger.info("事件标题:"       + applyItem.getTitle());
			logger.info("事件发生地址:"   +	applyItem.getPlace());
			logger.info("事件发生时间:"   + df.format(applyItem.getOccurTime()));
			logger.info("事件来源:"       + applyItem.getOrigin());
			logger.info("事件影响度:"     + applyItem.getIncidence());
			logger.info("事件优先级:"     + applyItem.getPriority());
			logger.info("事件完成期限:"   + df.format(applyItem.getFinishLimit()));
			logger.info("所属系统类型:"   + applyItem.getSysType());
			logger.info("事件内容:"       + applyItem.getContent());
			logger.info("附件标记:"       + applyItem.isAttachFlag());
			logger.info("附件数量:"       + applyItem.getFileNum());
			logger.info("附件名称:"       + StringUtils.join(applyItem.getAttName(), ","));
			
			inputXML =  "<?xml version=\"1.0\" encoding=\"GB2312\"?><root><data>"
				+ "<applySerial>" 		+ applyItem.getApplyserial() 					+ "</applySerial>"
				+ "<submitStaffName>"   + applyItem.getSubmitStaffName() 				+ "</submitStaffName>"
				+ "<title>" 			+ applyItem.getTitle() 							+ "</title>"
				+ "<place>" 			+ applyItem.getPlace() 							+ "</place>"
				+ "<occurTime>" 		+ df.format(applyItem.getOccurTime())			+ "</occurTime>"
				+ "<origin>" 			+ applyItem.getOrigin() 						+ "</origin>"
				+ "<incidence>" 		+ applyItem.getIncidence() 						+ "</incidence>"
				+ "<priority>" 			+ applyItem.getPriority() 						+ "</priority>"
				+ "<finishLimit>" 		+ df.format(applyItem.getFinishLimit()) 		+ "</finishLimit>"
				+ "<sysType>" 			+ applyItem.getSysType() 						+ "</sysType>"
				+ "<content>" 			+ applyItem.getContent() 						+ "</content>"
				+ "<attachFlag>"		+ applyItem.isAttachFlag() 						+ "</attachFlag>"
				+ "<fileNum>" 			+ applyItem.getFileNum() 						+ "</fileNum>"
				+ "<attName>" 			+ StringUtils.join(applyItem.getAttName(), ",")	+ "</attName>"
				+ "</data></root>"; 
			
			logger.info("派单输入信息:" + inputXML);
			resultXML = callBiz("spi", "bizITSM_Fault_Interface.bizITSM_BZ_PDintf", inputXML);
			logger.info("派单返回信息:" + resultXML);
			
			
		} catch (Exception e) {
			resultValue = "调用 服务保障系统工单派发接口失败,原因";
			resultValue = resultValue  + e.getMessage();
			logger.info("异常原因:" + resultValue);
			throw new AppException(resultValue, null);
		}
		
		//3、把派单返回信息转成Document对象并赋值给ApplyItemResponse->resultObj对象
		Document doc;
		try {
			doc = stringToDoc(resultXML);
			NodeList nodes = doc.getElementsByTagName("data");
			for (int i = 0; i < nodes.getLength(); i++) {
				Node node = nodes.item(i);
				NodeList childNodes = node.getChildNodes();
				for (int j = 0; j < childNodes.getLength(); j++) {
					Node childNode = childNodes.item(j);
					if (childNode.getNodeType() == Node.ELEMENT_NODE && childNode.getNodeName().equals("result"))
						resultObj.setResult(new Integer(childNode.getFirstChild().getNodeValue()));
					
					if (childNode.getNodeType() == Node.ELEMENT_NODE && childNode.getNodeName().equals("fwbzSerial"))
						resultObj.setFwbzSerial(childNode.getFirstChild().getNodeValue());
					
					if (childNode.getNodeType() == Node.ELEMENT_NODE && childNode.getNodeName().equals("itsmSerial"))
						resultObj.setItsmSerial(childNode.getFirstChild().getNodeValue());
					
					if (childNode.getNodeType() == Node.ELEMENT_NODE && childNode.getNodeName().equals("msg"))
						resultObj.setMsg(childNode.getFirstChild().getNodeValue());
				}
			}
		} catch (Exception e) {
			resultValue = "派单结果转换失败,原因";
			resultValue = resultValue  + e.getMessage();
			logger.info("异常原因:" + resultValue);
			throw new AppException(resultValue, null);
		}
		
		return resultObj;
	}

	......

	/**
	 * String 转 XML org.w3c.dom.Document
	 */
	private static Document stringToDoc(String xmlStr) {
		// 字符串转XML
		Document doc = null;
		try {
			xmlStr = new String(xmlStr.getBytes(), "gb2312");
			StringReader sr = new StringReader(xmlStr);
			InputSource is = new InputSource(sr);
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder;
			builder = factory.newDocumentBuilder();
			doc = builder.parse(is);
		} catch (ParserConfigurationException e) {
			System.err.println(xmlStr);
			e.printStackTrace();
		} catch (SAXException e) {
			System.err.println(xmlStr);
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println(xmlStr);
			e.printStackTrace();
		}
		return doc;
	}
	
	/**
	 * XML org.w3c.dom.Document 转 String
	 */
	private static String docToString(Document doc) {
		// XML转字符串
		String xmlStr = "";
		try {
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer t = tf.newTransformer();
			t.setOutputProperty("encoding", "gb2312");// 解决中文问题,试过用GBK不行UTF-8
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			t.transform(new DOMSource(doc), new StreamResult(bos));
			xmlStr = bos.toString();
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
		return xmlStr;
	}

}

 

 

package com.starit.itsmhd.service;
import java.rmi.RemoteException;

import javax.activation.DataHandler;

import org.w3c.dom.*;

import com.primeton.tp.core.api.BizContext;
import com.starit.itsmhd.bean.ApplyItem;
import com.starit.itsmhd.bean.ApplyItemResponse;
import com.starit.itsmhd.bean.CancelItem;
import com.starit.itsmhd.bean.CancelItemResponse;
import com.starit.itsmhd.bean.CloseItem;
import com.starit.itsmhd.bean.CloseItemResponse;
import com.starit.itsmhd.bean.PromptItem;
import com.starit.itsmhd.bean.PromptItemResponse;
import com.starit.itsmhd.bean.QueryItem;
import com.starit.itsmhd.bean.QueryItemResponse;
import com.starit.itsmhd.exception.AppException;
import com.starit.itsmhd.impl.FwbzImpl;
import com.starit.itsmhd.interfaces.FwbzInter;
/**
 * @author zqding
 * @version 1.0
 * @date 2010-11-22
 * @class_displayName FwbzService
 */

public class FwbzService implements FwbzInter {

	private FwbzInter impl;

	public FwbzService() {
		this.impl = new FwbzImpl();
	}

	public FwbzService(FwbzInter impl) {
		this.impl = impl;
	}

	//派单
	public ApplyItemResponse putEvent(ApplyItem applyItem,	DataHandler[] attchments) throws RemoteException, AppException {
		ApplyItemResponse ret = impl.putEvent(applyItem, attchments);
		return ret;
	}

	//查询工单
	public QueryItemResponse queryEvent(QueryItem queryItem) throws RemoteException, AppException {
		QueryItemResponse ret = impl.queryEvent(queryItem);
		return ret;
	}

	//取消工单
	public CancelItemResponse cancelEvent(CancelItem cancelItem) throws RemoteException, AppException {
		CancelItemResponse ret = impl.cancelEvent(cancelItem);
		return ret;
	}

	//关闭工单
	public CloseItemResponse closeEvent(CloseItem closeItem, DataHandler[] attchments) throws RemoteException, AppException {
		CloseItemResponse ret = impl.closeEvent(closeItem, attchments);
		return ret;
	}

	//催单
	public PromptItemResponse promptEvent(PromptItem promptItem) throws RemoteException, AppException {
		PromptItemResponse ret = impl.promptEvent(promptItem);
		return ret;
	}

}

 

 

4、测试代码:

package com.starit.itsmhd.testcase;

import java.io.File;
import java.rmi.RemoteException;
import java.util.Date;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory;
import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory;

import com.starit.itsmhd.bean.ApplyItem;
import com.starit.itsmhd.bean.ApplyItemResponse;
import com.starit.itsmhd.bean.CancelItem;
import com.starit.itsmhd.bean.CancelItemResponse;
import com.starit.itsmhd.bean.CloseItem;
import com.starit.itsmhd.bean.CloseItemResponse;
import com.starit.itsmhd.bean.PromptItem;
import com.starit.itsmhd.bean.PromptItemResponse;
import com.starit.itsmhd.bean.QueryItem;
import com.starit.itsmhd.bean.QueryItemResponse;
import com.starit.itsmhd.exception.AppException;

/**
 * @author zqding
 * @version 1.0
 * @date 2010-11-22
 * @class_displayName TestCase
 */

public class TestCase {

	public static void main(String[] args) {
		putSheet();
		......
	}

	public static int putSheet() {

		String[] fileNames = { "d:/111.pdf", "d:/111.rar" };

		ApplyItem item = new ApplyItem();
		item.setApplyserial("111111111");
		item.setSubmitStaffName("wangyongq");
		item.setTitle("测试单");
		item.setPlace("xxx");
		item.setOccurTime(new Date());
		item.setOrigin(11);
		item.setIncidence(12);
		item.setPriority(12);
		item.setFinishLimit(new Date());
		item.setSysType(10006);
		item.setContent("xxxx测试单");
		item.setFileNum(2);
		item.setAttName(fileNames);
		item.setAttachFlag(true);

		DataHandler[] dataHandler = new DataHandler[item.getFileNum()];

		for (int i = 0; i < item.getFileNum(); i++) {
			File file = new File(item.getAttName()[i]);
			FileDataSource source = new FileDataSource(file);
			dataHandler[i] = new DataHandler(source);
		}

		ApplyItemResponse response;// = new ApplyItemResponse();
		Call call;
		try {

			String url = "http://127.0.0.1:7001/axis/services/FwbzService";

			Service service = new Service();

			call = (Call) service.createCall();

			call.setTargetEndpointAddress(url);

			// 设定服务方法名
			call.setOperationName(new QName(url, "putEvent"));

			QName qna = new QName("http://itsmhd.starit.com/bean", "ApplyItem");

			QName qnd = new QName("http://itsmhd.starit.com/bean",
					"ArrayOf_tns1_DataHandler");

			QName returnType = new QName("http://itsmhd.starit.com/bean",
					"ApplyItemResponse");
			
			QName ae = new QName("http://itsmhd.starit.com/bean",
			"AppException");

			call.registerTypeMapping(ApplyItem.class, qna,
					new BeanSerializerFactory(ApplyItem.class, qna),
					new BeanDeserializerFactory(ApplyItem.class, qna));

			call.registerTypeMapping(DataHandler.class, qnd,
					JAFDataHandlerSerializerFactory.class,
					JAFDataHandlerDeserializerFactory.class);

			call.registerTypeMapping(ApplyItemResponse.class, returnType,
					new BeanSerializerFactory(ApplyItemResponse.class,returnType), 
					new BeanDeserializerFactory(ApplyItemResponse.class,returnType));
			
			call.registerTypeMapping(AppException.class, ae,
					new BeanSerializerFactory(AppException.class,ae), 
					new BeanDeserializerFactory(AppException.class,ae));

			// 设置参数占位符
			call.addParameter("arg1", qna, ParameterMode.IN);
			call.addParameter("arg2", qnd, ParameterMode.IN);			
			call.setReturnType(returnType);
			// 设置Webservice超时时间
			call.setTimeout(new Integer(10000));
			// 调用服务并获取服务调用返回信息
			response = (ApplyItemResponse) call.invoke(new Object[] { item,	dataHandler });
			System.out.println("调用结果:response>>>>>>>>>>>>>>>>>>>>>>>>" + response.getMsg());
		} catch (ServiceException e) {
			e.printStackTrace();
			System.out.println("service method exception!");
			// 调用Webservice异常时,设置标识值,直接返回
			return 2;
		} catch (AppException e) {
			e.printStackTrace();
			return 2;
		} catch (Exception e) {
			System.out.println("remote mothod exception!");
			e.printStackTrace();
			return 2;
		}

		return 1;

	}	
	.......
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics