客户端使用AXIS的CALL调用
一、客户端使用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;
- }
- }
相关推荐
mmmjyjy 2020-07-16
千锋 2020-07-04
jzlixiao 2020-05-04
babycrying 2009-12-11
zhangxiaojiakele 2020-01-07
lybbb 2019-12-25
newfarhui 2019-12-12
jiahaohappy 2019-12-04
BETTINA 2019-12-02
jianghuchuanke 2019-11-03
morexyoung 2019-11-01
Selier 2014-05-28
javaer 2016-12-06
kx0 2019-04-29
xiajlxiajl 2015-09-01
xiajlxiajl 2013-01-25
82580194 2015-06-13