webservice 的调用 服务端 参数 List<Object> 的实例,
webservice 的调用 服务端 参数 List<Object> 的实例,
RookieFang 发表于1年前
webservice 的调用 服务端 参数 List<Object> 的实例,
  • 发表于 1年前
  • 阅读 163
  • 收藏 0
  • 点赞 0
  • 评论 0

移动开发云端新模式探索实践 >>>   

摘要: 上篇已经记录了 客户端的 webservice,现在分开介绍下 服务端的编写

首先:jar包是一样的

然后server-config.wsdd 文件的编写,放在WEB-INF下面即可:

<?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="adminPassword" value="admin" />
		<parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl" />
		<parameter name="sendXsiTypes" value="true" />
		<parameter name="sendMultiRefs" value="true" />
		<parameter name="sendXMLDeclaration" value="true" />
		<parameter name="axis.sendMinimizedElements" 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" />
	<handler name="soapmonitor" type="java:org.apache.axis.handlers.SOAPMonitorHandler">
		<parameter name="wsdlURL" value="/axis/SOAPMonitorService-impl.wsdl"/>
		<parameter name="namespace" value="http://tempuri.org/wsdl/2001/12/SOAPMonitorService-impl.wsdl"/>
		<parameter name="serviceName" value="SOAPMonitorService"/>
		<parameter name="portName" value="Demo"/>
	</handler>
	<handler name="logging" type="java:com.aa.aa.util.LogHandler" />

	
	
	
	<service name="SaveDataServiceImpl" provider="java:RPC">
		<requestFlow>
			<handler type="soapmonitor" />
			<handler type="logging" />
		</requestFlow>
		<responseFlow>
			<handler type="soapmonitor"/>
		</responseFlow>
		<parameter name="allowedMethods" value="*" />
		<parameter name="className" value="com.aa.aa.savedata.ws.SaveDataServiceImpl" />

	</service>
	

	<transport name="http">
		<requestFlow>
			<handler type="URLMapper" />
			<handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler" />
		</requestFlow>
	</transport>
	<transport name="local">
		<responseFlow>
			<handler type="LocalResponder" />
		</responseFlow>
	</transport>
</deployment>

2、ws  即生成 服务器url的 类(com.aa.aa.savedata.ws.SaveDataServiceImpl)

package com.aa.aa.savedata.ws;

import java.util.TreeMap;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.aa.aa.savedata.service.SaveDataService;
import com.aa.aa.util.XMLRead;
/**
 * 
* @company 
* @author 
* @ClassName: SaveDataServiceImpl
* @Description:
* @date 2017
* @version 1.0
 */
public class SaveDataServiceImpl {
	
	private SaveDataService saveDataService;
	
	public String saveCheckData(String inXml){
		XMLRead reader = new XMLRead();
		StringBuffer sbBuf = new StringBuffer();
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");   
		saveDataService = (SaveDataService) context.getBean("saveDataService");
		System.out.println(saveDataService+"--------------------------");
		sbBuf.append("<?xml version='1.0' encoding='utf-8'?><tobacco action='saveCheckData'>");
		//解析xml
		TreeMap m = null;
		try {
			m = reader.parseXml(inXml);
			saveDataService.SaveDatamap(m);
			sbBuf.append("<dataset ifSuccess='1'/>");
			sbBuf.append("</tobacco>");
		} catch (Exception e) {
			sbBuf.append("<dataset ifSuccess='0'/>");
			sbBuf.append("</tobacco>");
			e.printStackTrace();
		}
		return sbBuf.toString();
	
	}
}

3、走 service (SaveDatamap)  此处 将 xml格式的 String 参数  转换为 我们需要的List格式参数

public void SaveDatamap(Map m) throws Exception{
		
		Object listobj = m.get("listJson");
		JSONArray listJson = JSONArray.fromObject(listobj );
		List<Object> list= (List<Object>)JSONArray.toCollection(listJson , Object.class);

      Object list1obj = m.get("list1Json");
		JSONArray list1Json = JSONArray.fromObject(list1obj );
		List<Object1> list1= (List<Object1>)JSONArray.toCollection(list1Json , Object1.class);




 

4、分享一下XML解析公用类(dom4j)

package com.aa.aa.util;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Attribute;
/**
 * XML解析公用类(dom4j)
 * @author Administrator
 */
public class XMLRead {
	private final Logger log = Logger.getLogger("I");
	private final Logger elog = Logger.getLogger("E");

	private TreeMap rmap = new TreeMap();
	private static String mapName = "mymap";
	private int cnum = 0;
	private static int level = 0;

	public String getMapName() {
		return mapName;
	}
	public Element getRootElement(String inXml) throws Exception {
		Element element = null;
		try {
			Document srcdoc = DocumentHelper.parseText(inXml);
			element = srcdoc.getRootElement();
		} catch (Exception e) {
			elog.error("解析XML文件[getRootElement]出错,请检查输入是否正确!\n" + inXml + "\n" + e.getMessage());// e.printStackTrace();
			throw new Exception(e.getMessage());
		}
		return element;
	}
	/** 递归遍历方法 */
	public void getElementList(Element element) {
		List elements = element.elements();
		level++;
		if (elements.size() == 0) {// no child
			String name = element.getName();
			String value = element.getTextTrim();
			if (name != null) {
				if ("DATASET_LINE".equals(name.toUpperCase())) {
					cnum++;
					rmap.put(name + cnum, value);
//					log.info("getElementList|elements=0| element |dl|rmap:" + rmap.size() + "|cnum:" + cnum + "|level:" + level + "|name:" + name + "|value:" + value);
					TreeMap cmap = new TreeMap();
					for (Iterator ait = element.attributeIterator(); ait.hasNext();) {
						Attribute attr = (Attribute) ait.next();
						String aname = attr.getName();
						String avalue = attr.getValue();
						cmap.put(aname, avalue);
//						log.info("getElementList|elements=0|attribute|dl|rmap:" + rmap.size() + "|cnum:" + cnum + "|level:" + level + "|name:" + aname + "|value:" + avalue);
					}
					rmap.put(mapName + cnum, cmap);
				} else {
					rmap.put(name, value);
//					log.info("getElementList|elements=0| element |rmap:" + rmap.size() + "|cnum:" + cnum + "|level:" + level + "|name:" + name + "|value:" + value);
					for(Iterator ait = element.attributeIterator(); ait.hasNext();){
						Attribute attr = (Attribute) ait.next();
						String aname = attr.getName();
						String avalue = attr.getValue();
						rmap.put(aname, avalue);
//						log.info("getElementList|elements=0|attribute|rmap:" + rmap.size() + "|cnum:" + cnum + "|level:" + level + "|name:" + aname + "|value:" + avalue);
					}
				}
			}
		} else {// have child
			String name = element.getName();
			String value = element.getTextTrim();
			rmap.put(name, value);
//			log.info("getElementList|elements>0| element |rmap:" + rmap.size() + "|cnum:" + cnum + "|level:" + level + "|name:" + name + "|value:" + value);
			for (Iterator ait = element.attributeIterator(); ait.hasNext();) {
				Attribute attr = (Attribute) ait.next();
				String aname = attr.getName();
				String avalue = attr.getValue();
				rmap.put(aname, avalue);
//				log.info("getElementList|elements>0|attribute|rmap:" + rmap.size() + "|cnum:" + cnum + "|level:" + level + "|name:" + aname + "|value:" + avalue);
			}
			for (Iterator it = elements.iterator(); it.hasNext();) {
				Element elem = (Element) it.next();
				getElementList(elem);// 递归遍历
			}
		}
	}
	public TreeMap parseXml(String inXml) throws Exception {
		try {
			Element root = getRootElement(inXml);
			if (root != null) {
				getElementList(root);
				rmap.put("DETAIL_NUM", String.valueOf(cnum));
			}
		} catch (Exception e) {
			elog.error("解析XML文件[parseXml]出错,请检查输入是否正确!\n" + inXml + "\n" + e.getMessage());// e.printStackTrace();
			throw new Exception(e.getMessage());
		}
		return rmap;
	}
	public void logMapInfo(Map m) {
		Iterator it = m.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			log.info(entry.getKey() + " = " + entry.getValue());
		}
	}

	public static void main(String[] args) throws DocumentException{
		String xml = "";
		xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
				"<tobacco action='getOrderStatus'>" +
					"<dataset client_code='clientCode' sell_date='sellDate' sell_type_code='sellTypeCode'/>" +
				"</tobacco>";
		/*xml = "<?xml version='1.0' encoding='utf-8'?>" +
				"<tobacco action='getPubInfo' code='0000' msg='数据处理成功!' trans_time='2011-07-04 01:54:18'>" +
					"<dataset rowcount='1' >" +
						"<dataset_line info_type='普通公告' create_date='2011-11-22' title='11月22日“黄渡江软盒”断货通知' content='test services1' />" +
						"<dataset_line info_type='普通公告' create_date='2011-11-21' title='11月21日上午10点30分“硬盒一品”断货通知' content='test services2' />" +
						"<dataset_line info_type='营销公告' create_date='2011-11-18' title='11月18日“硬中华”到货通知' content='&lt;test \\\\services3&gt;' />" +
					"</dataset>" +
				"</tobacco>";*/
		/*xml = "<?xml version='1.0' encoding='utf-8'?>\n" +
				"<tobacco action='getPubInfo'>\n" +
					"<dataset1 sell_date='2011-12-01'>\n" +
						"<dataset_line cig_code='xxxxxxxx' sell_amount='10' />\n" +
						"<dataset_line cig_code='yyyyyyyy' sell_amount='20' />\n" +
					"</dataset1>\n" +
					"<dataset2 sell_date='2011-12-02'>\n" +
						"<dataset_line cig_code='aaaaaaaa' sell_amount='30' />\n" +
						"<dataset_line cig_code='bbbbbbbb' sell_amount='40' />\n" +
					"</dataset2>\n" +
				"</tobacco>";*/
		XMLRead reader = new XMLRead();
		/**
		 * 无法递归
		 */
		try {
			reader.setSrcXML(xml);
			TreeMap m1 = reader.parseXML();
//			reader.logMapInfo(m1);

			int num_dataset = Integer.parseInt((String) m1.get(reader.getPNumDataSet()));
			for (int i = 1; i <= num_dataset; i++) {
				Map m = (Map) m1.get(reader.getPDataSet() + i);
				System.out.println(m.size() + "|" + m.get("client_code"));
			}

			/*Iterator it = m1.entrySet().iterator();
			String pDataSet = reader.getPDataSet(), pDataSetLine = reader.getPDataSetLine(), pNumDataSet = reader.getPNumDataSet(), pNumDataSetLine = reader.getPNumDataSetLine();
			while (it.hasNext()) {
				Map.Entry entry = (Map.Entry) it.next();
				String key = (String) entry.getKey();
				if (key.indexOf(pDataSet) >= 0) {
					TreeMap dsm = (TreeMap) entry.getValue();
					Iterator it1 = dsm.entrySet().iterator();
					while (it1.hasNext()) {
						Map.Entry entry1 = (Map.Entry) it1.next();
						String key1 = (String) entry1.getKey();
						if (key1.indexOf(pDataSetLine) >= 0) {
							TreeMap dslm = (TreeMap) entry1.getValue();
							Iterator it2 = dslm.entrySet().iterator();
							while (it2.hasNext()) {
								Map.Entry entry2 = (Map.Entry) it2.next();
								System.out.println("dataset_line|" + entry2.getKey() + " = " + entry2.getValue());
							}
						} else {
							System.out.println("dataset|" + key1 + " = " + entry1.getValue());
						}
					}
				} else {
					System.out.println("tobacco|" + key + " = " + entry.getValue());
				}
			}*/
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.exit(0);

		/**
		 * 递归
		 */
		TreeMap m = null;
		try {
			m = reader.parseXml(xml);
			reader.logMapInfo(m);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 遍历map
		Iterator it = m.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String key = (String) entry.getKey();
			if (key.indexOf(reader.getMapName()) >= 0) {
				TreeMap cm = (TreeMap) entry.getValue();
				Iterator it1 = cm.entrySet().iterator();
				while (it1.hasNext()) {
					Map.Entry entry1 = (Map.Entry) it1.next();
					String key1 = (String) entry1.getKey();
					String value1 = (String) entry1.getValue();
					System.out.println(key1 + " = " + value1);
				}
			}else{
				String value = (String) entry.getValue();
				System.out.println(key + " = " + value);
			}
		}
	}

	// 非递归方法
	private String srcXML = "<?xml version='1.0' encoding='utf-8'?>\n" +
		"<tobacco action='getSellInfo'>\n" +
			"<dataset1 sell_no='90153190' sell_date='2011-12-01' fill_date='2011-11-28 18:12:52.259898' sell_type_code='16704' cig_count='13' sum_indent_amount='30' sum_indent_money='3047.00' sum_sell_amount='30' sum_sell_money='3047.00' f_status='3' >\n" +
				"<dataset_line cig_code='021053037' cig_name='黄山(国宾迎客松)硬盒' package_name='条' tax_price='180.00' need_amount='1' s_indent_amount='1' indent_money='180.00' s_sell_amount='1' sell_money='180.00' />\n" +
				"<dataset_line cig_code='021053042' cig_name='黄山(新制皖烟)硬盒' package_name='条' tax_price='122.00' need_amount='5' s_indent_amount='5' indent_money='610.00' s_sell_amount='5' sell_money='610.00' >\n" +
					"<limit_amount>10</limit_amount>" +
					"<ret_money ret_flag='false'>20.00</ret_money>" +
				"</dataset_line>" +
				"<dataset_line>\n" +
					"<cig_code>021053043</cig_code>" +
					"<cig_name>黄山(贵宾迎客松)硬盒</cig_name>" +
					"<package_name>条</package_name>" +
					"<tax_price>92.00</tax_price>" +
					"<need_amount>6</need_amount>" +
					"<s_indent_amount>5</s_indent_amount>" +
					"<indent_money>460.00</indent_money>" +
					"<s_sell_amount>5</s_sell_amount>" +
					"<sell_money>460.00</sell_money>" +
					"<limit_amount>5</limit_amount>" +
				"</dataset_line>" +
			"</dataset1>\n" +
			"<dataset2 sell_no='90161082' sell_date='2011-12-08' fill_date='2011-12-05 15:01:18.604880' sell_type_code='16704' cig_count='1' sum_indent_amount='5' sum_indent_money='460.00' sum_sell_amount='5' sum_sell_money='460.00' f_status='3' >\n" +
				"<dataset_line cig_code='021053043' cig_name='黄山(贵宾迎客松)硬盒' package_name='条' tax_price='92.00' need_amount='5' s_indent_amount='5' indent_money='460.00' s_sell_amount='5' sell_money='460.00' />\n" +
			"</dataset2>\n" +
		"</tobacco>";
	private String pNumDataSet = "num_dataset";
	private String pNumDataSetLine = "num_ds_line";
	private String pDataSet = "map_dataset";
	private String pDataSetLine = "map_ds_line";
	public String getSrcXML() {
		return srcXML;
	}
	public TreeMap parseXML() {
		StringBuffer sblog = new StringBuffer();
		TreeMap tobaccoMap = new TreeMap();
		int numDataSet = 0;
		try {
			Document document = DocumentHelper.parseText(srcXML);
			// element: tobacco
			Element root = document.getRootElement();
			sblog.append("------------开始------------------");
			if (root != null) {
				sblog.append("\nroot elements size:").append(root.elements().size()).append(" | attributes size:").append(root.attributes().size()).append(" | elements info:").append(root);
				// attribute: tobacco
				if (root.attributes().size() > 0) {
					sblog.append("\nroot attribute : ");
					for (Iterator it_a_tobacco = root.attributeIterator(); it_a_tobacco.hasNext();) {
						Attribute attr_tobacco = (Attribute) it_a_tobacco.next();
						String name_tobacco = attr_tobacco.getName();
						String value_tobacco = attr_tobacco.getValue();
						sblog.append(name_tobacco).append("=").append(value_tobacco).append(" | ");
						tobaccoMap.put(name_tobacco, value_tobacco);
					}
				}

				if (root.elements().size() > 0) {
					for (Iterator it_e_tobacco = root.elementIterator(); it_e_tobacco.hasNext();) {
						// element: tobacco -> dataset
						Element dataset = (Element) it_e_tobacco.next();
						numDataSet++;
						TreeMap dataSetMap = new TreeMap();
						int numDataSetLine = 0;

						sblog.append("\n\tdataset elements size:").append(dataset.elements().size()).append(" | attributes size:").append(dataset.attributes().size()).append(" | elements info:").append(dataset);
						// attribute: tobacco -> dataset
						if (dataset.attributes().size() > 0) {
							sblog.append("\n\tdataset attribute : ");
							for (Iterator it_a_dataset = dataset.attributeIterator(); it_a_dataset.hasNext();) {
								Attribute attr_dataset = (Attribute) it_a_dataset.next();
								String name_dataset = attr_dataset.getName();
								String value_dataset = attr_dataset.getValue();
								sblog.append(name_dataset).append("=").append(value_dataset).append(" | ");
								dataSetMap.put(name_dataset, value_dataset);
							}
						}

						if (dataset.elements().size() > 0) {
							for (Iterator it_e_dataset = dataset.elementIterator(); it_e_dataset.hasNext();) {
								// element: tobacco -> dataset
								Element datasetline = (Element) it_e_dataset.next();
								numDataSetLine++;
								TreeMap dataSetLineMap = new TreeMap();
								sblog.append("\n\t\tdatasetline elements size:").append(datasetline.elements().size()).append(" | attributes size:").append(datasetline.attributes().size()).append(" | elements info:").append(datasetline);
								// attribute: tobacco -> dataset
								if (datasetline.attributes().size() > 0) {
									sblog.append("\n\t\tdatasetline attribute : ");
									for (Iterator it_a_datasetline = datasetline.attributeIterator(); it_a_datasetline.hasNext();) {
										Attribute attr_datasetline = (Attribute) it_a_datasetline.next();
										String name_datasetline = attr_datasetline.getName();
										String value_datasetline = attr_datasetline.getValue();
										sblog.append(name_datasetline).append("=").append(value_datasetline).append(" | ");
										dataSetLineMap.put(name_datasetline, value_datasetline);
									}
								}

								if (datasetline.elements().size() > 0) {
									for (Iterator it_e_datasetline = datasetline.elementIterator(); it_e_datasetline.hasNext();) {
										Element other = (Element) it_e_datasetline.next();
										sblog.append("\n\t\t\tother elements size:").append(other.elements().size()).append(" | attributes size:").append(other.attributes().size()).append(" | elements info:").append(other);
										if (other.attributes().size() > 0) {
											sblog.append("\n\t\t\tother attribute : ");
											for (Iterator it_a_other = other.attributeIterator(); it_a_other.hasNext();) {
												Attribute attr_other = (Attribute) it_a_other.next();
												String name_other = attr_other.getName();
												String value_other = attr_other.getValue();
												sblog.append(name_other).append("=").append(value_other).append(" | ");
												dataSetLineMap.put(name_other, value_other);
											}
										}
										if (other.elements().size() > 0) {
											elog.error("XML层级超过处理范围,丢失数据!");
										} else {
											String name_other = other.getName();
											String value_other = other.getText();
											sblog.append(name_other).append("=").append(value_other).append(" | ");
											dataSetLineMap.put(name_other, value_other);
										}
									}
								}

								dataSetMap.put(this.pDataSetLine + numDataSetLine, dataSetLineMap);
							}
						}

						dataSetMap.put(this.pNumDataSetLine, String.valueOf(numDataSetLine));
						tobaccoMap.put(this.pDataSet + numDataSet, dataSetMap);
					}
				}
			}
			sblog.append("\n ------------结束------------------");
//			log.info(sblog);
		} catch (Exception e) {
			e.printStackTrace();
		}
		tobaccoMap.put(this.pNumDataSet, String.valueOf(numDataSet));
		return tobaccoMap;
	}
	public void setSrcXML(String srcXML) {
		this.srcXML = srcXML;
	}
	public String getPNumDataSet() {
		return pNumDataSet;
	}
	public String getPNumDataSetLine() {
		return pNumDataSetLine;
	}
	public String getPDataSet() {
		return pDataSet;
	}
	public String getPDataSetLine() {
		return pDataSetLine;
	}
}

 

 

 

 

 

 

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 3
博文 29
码字总数 48758
×
RookieFang
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: