江湖开发者 | Java粉
网站地图
首页> Java> axis2 传输简单对象
2016
05-02

axis2 传输简单对象

axis2 传输简单对象


接口方法

    public User getUser(User user){
        System.out.println(user.getAge()+"age");
        System.out.println(user.getName()+"name");
//      user.setAge("100");
//      user.setName("100000");
        return user;
    }

 rpc 方式传输User对象,User implements Serializable 

public static void main(String[] args) throws AxisFault {
        String url ="http://127.0.0.1:8080/webservices-axis2/services/HelloWorld";
        String method ="getUser";
         
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
        EndpointReference targetEPR = new EndpointReference(url);
        options.setTo(targetEPR);
        QName opAddEntry = new QName("http://services.jhkz.com",method);
        User user = new User();
        user.setName("少年,");
        user.setAge("18");
        Object[] opAddEntryArgs = new Object[] {user};
        Class[] classes = new Class[] { User.class };
        User ruser = (User) serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0];
        System.out.println(ruser.getName()+"_"+ruser.getAge()); 
    }

axiom 方式传输User对象

package com.jhkz.client;
 
import java.util.Iterator;
import javax.xml.namespace.QName;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNode;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.databinding.utils.BeanUtil;
import org.apache.axis2.engine.DefaultObjectSupplier;
 
import com.jhkz.model.User;
 
public class DocumentClientUser {
     
     
    public static void main(String[] args) throws AxisFault {
         
        EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:8080/webservices-axis2/services/HelloWorld?wsdl" );  
               Options options = new Options();  
               options.setTo(targetEPR);  
                  
                //加载目标服务器  
               ServiceClient sender = new ServiceClient();  
               sender.setOptions(options);  
                  
               OMElement result = sender.sendReceive(getBody());  
               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 u1 = (User) BeanUtil.processObject(omElement, User.class, null, true, new DefaultObjectSupplier(),null);  
                               System.out.println(u1.getName()+"_"+u1.getAge());  
                          }  
                    }  
               }  
    }
     
     private static OMElement getBody() {  
            QName qn1 = new QName("http://services.jhkz.com", "getUser");  
            QName qn2 = new QName("http://services.jhkz.com", "user");  
            
            User u = new User();
            u.setName("小明");
            u.setAge("19");
            OMElement ome = BeanUtil.getOMElement(qn1, new Object[] { u }, qn2,  
                    true, null); 
            System.out.println("ome:"+ome);  
            return ome;  
        }  
      
     /**
      * ome:<getUser xmlns="http://services.jhkz.com"><user><age>19</age><name>小明</name></user></getUser>
        clientResult=<ns:getUserResponse xmlns:ns="http://services.jhkz.com"><ns:return xmlns:ax21="http://model.jhkz.com/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax21:User"><ax21:age>19</ax21:age><ax21:name>小明</ax21:name></ns:return></ns:getUserResponse>
        ok
        ***=return
        小明_19
 
      */
}


Java江湖     
全部评论:

表情验证码,看不清楚,换一张

随机文章

云标签

公众号

微信

分享:分享我们的知识;专注:专注个人技术的提升;