客户端使用AXIS的CALL调用

Andrewtao00 2013-07-10

一、客户端使用AXIS的CALL调用

package com.common;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.message.MessageElement;
import org.apache.axis.types.Schema;
import org.apache.axis2.databinding.utils.BeanUtil;
import com.shun.ebs.model.Senddividendstrategy;

public class Test3 {
	public final static String bipEndpoint = "http://192.168.10.227/ebsService/services/ebsService?wsdl";
	
	public final static String bipNamespace = "http://service.ebs.shun.com";

	public static void main(String[] args) {
		// queryAvailableAmt();
		// cancelDividendStratery();
		senddividendstrategy();
	}
	
	public static void senddividendstrategy() {
		try {
			Service service = new Service();
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(bipEndpoint));
			call.setOperationName(new QName(bipNamespace, "senddividendstrategy"));
			call.addParameter("in", XMLType.XSD_STRING, ParameterMode.IN);
			call.setEncodingStyle("UTF-8");
			call.setReturnType(XMLType.XSD_SCHEMA);
			OMElement ome = getOMFactory();
			String beanXml = ome.toStringWithConsume();
			System.out.println("客户端发送:"+beanXml);
			Object o = call.invoke(new Object[] { beanXml });
			Schema schema = (Schema) o;
			MessageElement[] messageElements = schema.get_any();
			StringBuffer str = new StringBuffer("");
			for (MessageElement m : messageElements) {
				str.append(m.toString());
			}
			System.out.println("客户端接受:"+str);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void cancelDividendStratery() {
		try {
			Service service = new Service();
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(bipEndpoint));
			call.setOperationName(new QName(bipNamespace, "cancelDividendStratery"));
			call.addParameter("p_company_code", XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter("p_dividend_date", XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter("p_equity_note_number", XMLType.XSD_STRING, ParameterMode.IN);
			call.setEncodingStyle("UTF-8");
			call.setReturnType(XMLType.XSD_SCHEMA);
			Object o = call.invoke(new Object[] { "801000", "2013-01-04", "" });
			Schema schema = (Schema) o;
			MessageElement[] messageElements = schema.get_any();
			StringBuffer str = new StringBuffer("");
			for (MessageElement m : messageElements) {
				str.append(m.toString());
			}
			System.out.println(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void queryAvailableAmt() {
		try {
			Service service = new Service();
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(bipEndpoint));
			call.setOperationName(new QName(bipNamespace, "queryAvailableAmt"));
			call.addParameter("p_dividend_date", XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter("p_company_code", XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter("p_user_name", XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter("p_password", XMLType.XSD_STRING, ParameterMode.IN);
			call.setEncodingStyle("UTF-8");
			call.setReturnType(XMLType.XSD_SCHEMA);
			Object o = call.invoke(new Object[] { "2013-01-04", "801000", "", "" });
			Schema schema = (Schema) o;
			MessageElement[] messageElements = schema.get_any();
			StringBuffer str = new StringBuffer("");
			for (MessageElement m : messageElements) {
				str.append(m.toString());
			}
			System.out.println(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static OMElement getOMFactory() {
		OMFactory fac = OMAbstractFactory.getOMFactory();
		// OMNamespace指定此SOAP文档名称空间
		OMNamespace om = fac.createOMNamespace("http://service.eee.shun.com", "client");
		// 创建元素,并指定其在om指代的名称空间中,元素名必须跟services.xml重大operation的name相同
		OMElement method = fac.createOMElement("senddividendstrategy", om);
		OMElement root;
		OMElement svsListOmElement;
		List<Senddividendstrategy> svsList = new ArrayList<Senddividendstrategy>();
		try {
			Senddividendstrategy s = null;
			for (int i = 0; i < 10; i++) {
				s = new Senddividendstrategy();
				s.setDividend_date("2013-01-04");
				s.setCompany_code("2");
				s.setEquity_note_number("3");
				s.setDividend_line_number(Long.valueOf(4));
				s.setAssign_type_code("5");
				s.setAssign_source("6");
				s.setSegment3("7");

				s.setAmount(Long.valueOf(8));
				s.setCash_amount(Long.valueOf(9));
				s.setCash_proportion(Long.valueOf(10));
				s.setCash_person_proportion(Long.valueOf(11));
				s.setCapital_source("12");
				s.setCapital_amount(Long.valueOf(13));
				s.setCapital_proportion(Long.valueOf(14));

				s.setCapital_person_proportion(Long.valueOf(15));
				s.setEquity_category("16");
				s.setEquity_properties("17");
				s.setReduce_tax_amount(Long.valueOf(18));
				s.setStart_date("2013-01-04");

				s.setManager_type("20");
				s.setProcess_startus("21");
				s.setData_status("22");

				svsList.add(s);
			}
			root = fac.createOMElement("root", null);
			svsListOmElement = BeanUtil.getOMElement(new QName("sparams"), svsList.toArray(), new QName("sparam"), false, null);
			root.addChild(svsListOmElement);
			method.addChild(root);

			return method;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

二、遇到的问题

客户端传送String类型的XML内容的时候服务器段接受会遇到需要处理(目的是要传送XML格式的数据)

public static OMElement checkDataByOMElement(OMElement element) throws AxisFault, Exception {
		String str = element.toStringWithConsume();
		str = str.replace("&lt;", "<")
				.replace("&gt;", ">")
				.replace("<in xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xsd:string\">", "")
				.replace("</in>", "")
				.replace("<client:senddividendstrategy xmlns:client=\"http://service.ebs.shun.com\">", "")
				.replace("</client:senddividendstrategy>", "");
		element = new StAXOMBuilder(new ByteArrayInputStream(str.getBytes("UTF-8"))).getDocumentElement();
		System.out.println("服务端接受:"+element.toString());
		System.out.println("服务端接受str:"+str);
.....

三、xml、OMElement、java对象之间转换

  1. import java.io.ByteArrayInputStream;  
  2.   
  3. import org.apache.axiom.om.OMElement;  
  4. import org.apache.axiom.om.impl.builder.StAXOMBuilder;  
  5. import org.apache.axis2.databinding.utils.BeanUtil;  
  6. import org.apache.axis2.engine.DefaultObjectSupplier;  
  7. import org.dom4j.Document;  
  8. import org.dom4j.DocumentHelper;  
  9.   
  10. public class XMLUtil  
  11. {  
  12.       
  13.     @SuppressWarnings("unchecked")  
  14.     public static  T xmlToBean(String xml, Class cls)  
  15.     {  
  16.         T object = null;  
  17.         try  
  18.         {  
  19.             OMElement omElement = new StAXOMBuilder(new ByteArrayInputStream(  
  20.                     xml.getBytes("UTF-8"))).getDocumentElement();  
  21.             object = (T) BeanUtil.processObject(omElement, cls, null, true,  
  22.                     new DefaultObjectSupplier());  
  23.         }  
  24.         catch (Exception e)  
  25.         {  
  26.             e.printStackTrace();  
  27.         }  
  28.         return object;  
  29.     }  
  30.       
  31.       
  32.     @SuppressWarnings("unchecked")  
  33.     public static  T xmlToBean(String xml, String elementName, Class cls)  
  34.     {  
  35.         T object = null;  
  36.         try  
  37.         {  
  38.             Document document = DocumentHelper.parseText(xml);  
  39.             String beanXml = document.getRootElement().element("Body").element(  
  40.                     elementName).asXML();  
  41.             OMElement omElement = new StAXOMBuilder(new ByteArrayInputStream(  
  42.                     beanXml.getBytes("UTF-8"))).getDocumentElement();  
  43.               
  44.             object = (T) BeanUtil.processObject(omElement, cls, null, true,  
  45.                     new DefaultObjectSupplier());  
  46.         }  
  47.         catch (Exception e)  
  48.         {  
  49.             e.printStackTrace();  
  50.         }  
  51.         return object;  
  52.     }  
  53. }  

相关推荐