axis自定义对象序列化

reverie001 贡献于2010-12-25

作者 Go7Z  创建于2010-10-20 14:41:00   修改者Go7Z  修改于2010-10-20 14:46:00字数4755

文档摘要:axis自定义对象序列化,只使用了beanMapping,未说明typeMapping,arrayMapping (参考axis user guide) 基于Axis的自定义对象序列化 1. 在Axis服务端,注册自定义对象的序列化/反序列化器主要是修改axis安装目录下WEB-INF/server-config.wsdd文件。
关键词:

axis 自定义对象序列化,只使用了beanMapping,未说明typeMapping,arrayMapping (参考axis user guide) 基于Axis的自定义对象序列化 1. 在Axis服务端,注册自定义对象的序列化/反序列化器 主要是修改axis安装目录下WEB-INF/server-config.wsdd文件。 Xml代码 1.    2.        3.      4.      6.     注意service/beanMapping的定义。languageSpecificType中定义使用那个类, qname定义类的名称。 2.  在Axis客户端,注册自定义对象的序列化/反序列化器。 核心代码如下: Java代码 1. //创建调用对象    2. Service service = new Service();    3. Call call = null;    4. call = (Call) service.createCall();    5.      6. //注册SimpleObject的序列化类型    7. QName qn = new QName("urn:BeanService","SimpleObject");    8. call.registerTypeMapping(SimpleObject.class, qn,    9.         new BeanSerializerFactory(SimpleObject.class, qn),    10.         new BeanDeserializerFactory(SimpleObject.class, qn));    //创建调用对象 Service service = new Service(); Call call = null; call = (Call) service.createCall(); //注册SimpleObject的序列化类型 QName qn = new QName("urn:BeanService","SimpleObject"); call.registerTypeMapping(SimpleObject.class, qn, new BeanSerializerFactory(SimpleObject.class, qn), new BeanDeserializerFactory(SimpleObject.class, qn)); 全部代码示例如下: Java代码 1. //SimpleObject.java    2. //Web Service对象传递的对象,必需继承Serializable    3. package com.whcyit.axis;    4. import java.io.Serializable;    5. public class SimpleObject implements Serializable {           6.        private static final long serialVersionUID = -6414428095965735488L;           7.        private int i = 0;    8.        private float f = 0.0f;    9.        private String str = "";    10.        public SimpleObject() {     11.        }    12.        public float getFloat() {    13.               return f;    14.        }    15.        public void setFloat(float f) {    16.               this.f = f;    17.        }       public int getInt() {    18.               return i;    19.       }     20.        public void setInt(int i) {     21.               this.i = i;     22.        }     23.        public String getString() {     24.               return str;    25.        }    26.      27.        public void setString(String str) {    28.               this.str = str;     29.        }     30. }    31.      32.    33.      34. // SimpleObjectService.java     35. //服务程序     36. package com.whcyit.axis;     37. public class SimpleObjectService {    38.        public String sayHello(String name) {    39.               return "Hello " + name +"!";    40.        }    41.      42.        public String getString(SimpleObject obj) {    43.               System.out.println("SimpleObjectService getString invoked!");     44.               return "return by serviced:"+obj.getString();    45.        }    46.      47.        public SimpleObject createSimpleObject(int i,float f,String str) {    48.               System.out.println("SimpleObjectService createSimpleObject invoked!");                   49.           SimpleObject so = new SimpleObject();    50.               so.setInt(i);    51.               so.setFloat(f);     52.               so.setString(str);     53.               return so;     54.        }    55.  }    56.      57.      58. // SimpleObjectClient.java    59. //客户端调用文件     60. package com.whcyit.axis;     61. import javax.xml.namespace.QName;     62. import org.apache.axis.client.Call;     63. import org.apache.axis.client.Service;     64. import org.apache.axis.encoding.ser.BeanDeserializerFactory;    65. import org.apache.axis.encoding.ser.BeanSerializerFactory;    66.      67. public class SimpleObjectClient {    68.      69.        public static void main(String[] args) {    70.               String ret = "";    71.               try {    72.                      String wsdlUrl = "http://localhost:8080/axis/services/SimpleObjectService?wsdl";     73.            String nameSpaceUri = "http://localhost:8080/axis/services/SimpleObjectService";     74.      75.                      //创建调用对象     76.                      Service service = new Service();     77.                      Call call = null;     78.                      call = (Call) service.createCall();                    79.      80.      81.                      //注册SimpleObject的序列化类型     82.                      QName qn = new QName("urn:BeanService","SimpleObject");    83.                      call.registerTypeMapping(SimpleObject.class, qn,     84.                                    new BeanSerializerFactory(SimpleObject.class, qn),     85.                                    new BeanDeserializerFactory(SimpleObject.class, qn));     86.                      //调用sayHello    87.                      System.out.println(">>>sayHello");    88.                      call.setOperationName(new QName(nameSpaceUri, "sayHello"));     89.                      call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));     90.                      ret = (String) call.invoke(new Object[] { "liu2" });    91.                      System.out.println("return value is " + ret);    92.      93.                      //调用getDesc     94.                      System.out.println(">>>getString");     95.                      SimpleObject so = new SimpleObject();     96.                      so.setString("hello!");     97.                      QName getDescQn = new QName(nameSpaceUri, "getString");     98.      99.                      call.setOperationName(getDescQn);    100.                      call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));     101.                      ret = (String) call.invoke(new Object[] { so });     102.                      System.out.println("return value is " + ret);                     103.      104.      105.                      //调用createSimpleObject    106.                      System.out.println(">>>createSimpleObject");     107.                      QName getObjectQn = new QName(nameSpaceUri, "createSimpleObject");     108.                      call.setOperationName(getObjectQn);     109.                      call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));     110.                      SimpleObject rtnSO = (SimpleObject) call.invoke(new Object[]    111.                  { new Integer(1),new Float(0.1f),"liu2" });     112.                      System.out.println("return object " + rtnSO.getString());     113.               } catch (Exception ex) {     114.                      ex.printStackTrace();     115.               }     116.       }     117. }   //SimpleObject.java //Web Service对象传递的对象,必需继承Serializable package com.whcyit.axis; import java.io.Serializable; public class SimpleObject implements Serializable { private static final long serialVersionUID = -6414428095965735488L; private int i = 0; private float f = 0.0f; private String str = ""; public SimpleObject() { } public float getFloat() { return f; } public void setFloat(float f) { this.f = f; } public int getInt() { return i; } public void setInt(int i) { this.i = i; } public String getString() { return str; } public void setString(String str) { this.str = str; } } // SimpleObjectService.java //服务程序 package com.whcyit.axis; public class SimpleObjectService { public String sayHello(String name) { return "Hello " + name +"!"; } public String getString(SimpleObject obj) { System.out.println("SimpleObjectService getString invoked!"); return "return by serviced:"+obj.getString(); } public SimpleObject createSimpleObject(int i,float f,String str) { System.out.println("SimpleObjectService createSimpleObject invoked!"); SimpleObject so = new SimpleObject(); so.setInt(i); so.setFloat(f); so.setString(str); return so; } } // SimpleObjectClient.java //客户端调用文件 package com.whcyit.axis; import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.ser.BeanDeserializerFactory; import org.apache.axis.encoding.ser.BeanSerializerFactory; public class SimpleObjectClient { public static void main(String[] args) { String ret = ""; try { String wsdlUrl = "http://localhost:8080/axis/services/SimpleObjectService?wsdl"; String nameSpaceUri = "http://localhost:8080/axis/services/SimpleObjectService"; //创建调用对象 Service service = new Service(); Call call = null; call = (Call) service.createCall(); //注册SimpleObject的序列化类型 QName qn = new QName("urn:BeanService","SimpleObject"); call.registerTypeMapping(SimpleObject.class, qn, new BeanSerializerFactory(SimpleObject.class, qn), new BeanDeserializerFactory(SimpleObject.class, qn)); //调用sayHello System.out.println(">>>sayHello"); call.setOperationName(new QName(nameSpaceUri, "sayHello")); call.setTargetEndpointAddress(new java.net.URL(wsdlUrl)); ret = (String) call.invoke(new Object[] { "liu2" }); System.out.println("return value is " + ret); //调用getDesc System.out.println(">>>getString"); SimpleObject so = new SimpleObject(); so.setString("hello!"); QName getDescQn = new QName(nameSpaceUri, "getString"); call.setOperationName(getDescQn); call.setTargetEndpointAddress(new java.net.URL(wsdlUrl)); ret = (String) call.invoke(new Object[] { so }); System.out.println("return value is " + ret); //调用createSimpleObject System.out.println(">>>createSimpleObject"); QName getObjectQn = new QName(nameSpaceUri, "createSimpleObject"); call.setOperationName(getObjectQn); call.setTargetEndpointAddress(new java.net.URL(wsdlUrl)); SimpleObject rtnSO = (SimpleObject) call.invoke(new Object[] { new Integer(1),new Float(0.1f),"liu2" }); System.out.println("return object " + rtnSO.getString()); } catch (Exception ex) { ex.printStackTrace(); } } } /server-config.wsdd //服务端配置文件 Xml代码 1.    2.    4.    5.       6.        7.        8.      10.     11.    12.      13.        14.      15.      16.   

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 5 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档