一、客户端使用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("<", "<")
- .replace(">", ">")
- .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对象之间转换
- import java.io.ByteArrayInputStream;
- import org.apache.axiom.om.OMElement;
- import org.apache.axiom.om.impl.builder.StAXOMBuilder;
- import org.apache.axis2.databinding.utils.BeanUtil;
- import org.apache.axis2.engine.DefaultObjectSupplier;
- import org.dom4j.Document;
- import org.dom4j.DocumentHelper;
- public class XMLUtil
- {
- @SuppressWarnings("unchecked")
- public static T xmlToBean(String xml, Class cls)
- {
- T object = null;
- try
- {
- OMElement omElement = new StAXOMBuilder(new ByteArrayInputStream(
- xml.getBytes("UTF-8"))).getDocumentElement();
- object = (T) BeanUtil.processObject(omElement, cls, null, true,
- new DefaultObjectSupplier());
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return object;
- }
- @SuppressWarnings("unchecked")
- public static T xmlToBean(String xml, String elementName, Class cls)
- {
- T object = null;
- try
- {
- Document document = DocumentHelper.parseText(xml);
- String beanXml = document.getRootElement().element("Body").element(
- elementName).asXML();
- OMElement omElement = new StAXOMBuilder(new ByteArrayInputStream(
- beanXml.getBytes("UTF-8"))).getDocumentElement();
- object = (T) BeanUtil.processObject(omElement, cls, null, true,
- new DefaultObjectSupplier());
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return object;
- }
- }