深入剖析Axis2中返回值为复杂自定义类型值【步步逼近实践篇】
- - CSDN博客推荐文章 问:Axis2中到底能否传递复杂以及自定义对象. 在开发过程中,基本类型已经不能满足要求. 所以才有有相应的List,Map,以及User对象,Dog对象等等. 比如传递User对象,我们想象一下,到底我们该怎么样,把这个对象传递过去呢. 我们再根据前面讲的Webservice传递协议,该如何办呢.
<xs:complexType name="User"> <xs:sequence> <xs:element minOccurs="0" name="age" typke="xs:int" /> <xs:element minOccurs="0" name="id" type="xs:long" /> <xs:element minOccurs="0" name="name" nillable="true" type="xs:string" /> </xs:sequence> </xs:complexType>
public User getUser() { User u= new User(); u.setId(100); u.setName( "123ee"); return u; }
public static void main(String[] args) throws Exception { EndpointReference targetEPR = new EndpointReference("http://localhost:8080/testWSServerByAxis2/services/myService?wsdl" ); try { Options options = new Options(); options.setTo(targetEPR); //加载目标服务器 ServiceClient sender = new ServiceClient(); sender.setOptions(options); OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace("http://test.com" , "" ); //调用服务端的方法 OMElement method = fac.createOMElement("getUser" , omNs); //给方法赋值参数值 //method.addChild(sayHello); //返回OMElement对象 OMElement result = sender.sendReceive(method); System. out.println( "clientResult="+result); Iterator iterator=result.getChildElements(); while(iterator.hasNext()){ System. out.println( "ok"); OMNode omNode = (OMNode) iterator.next(); if(omNode.getType()==omNode. ELEMENT_NODE){ OMElement omElement = (OMElement) omNode; System. out.println( "***="+omElement.getLocalName()); if (omElement.getLocalName().toLowerCase().equals("return" )) { User u = (User) BeanUtil.processObject(omElement, User. class, null, true, new DefaultObjectSupplier()); System. out.println(u.getName()); } } } } catch (Exception e) { e.printStackTrace(); } }
System. out.println( "clientResult="+result); clientResult=<ns:getUserResponse xmlns:ns="http://test.com"><ns:return xmlns:ax21="http://entity.com/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax21:User"><ax21:age>0</ax21:age><ax21:id>100</ax21:id><ax21:name>123ee</ax21:name></ns:return></ns:getUserResponse>