axis 自定义对象序列化,只使用了beanMapping,未说明typeMapping,arrayMapping (参考axis user guide)
基于Axis的自定义对象序列化
1. 在Axis服务端,注册自定义对象的序列化/反序列化器
主要是修改axis安装目录下WEB-INF/server-config.wsdd文件。
<service name="SimpleObjectService" provider="java:RPC">
<parameter name="allowedMethods" value="*"/>
<parameter name="className" value="com.whcyit.axis.SimpleObjectService"/>
<beanMapping languageSpecificType="java:com.whcyit.axis.SimpleObject"
qname="ns:SimpleObject" xmlns:ns="urn:BeanService"/>
</service>
注意service/beanMapping的定义。languageSpecificType中定义使用那个类, qname定义类的名称。
2. 在Axis客户端,注册自定义对象的序列化/反序列化器。
核心代码如下:
//创建调用对象
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));
全部代码示例如下:
//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 version="1.0" encoding="UTF-8"?>
<deployment xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"
xmlns="http://xml.apache.org/axis/wsdd/">
<handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/>
<service name="SimpleObjectService" provider="java:RPC">
<parameter name="allowedMethods" value="*"/>
<parameter name="className" value="com.whcyit.axis.SimpleObjectService"/>
<beanMapping languageSpecificType="java:com.whcyit.axis.SimpleObject"
qname="ns:SimpleObject" xmlns:ns="urn:BeanService"/>
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper"/>
</requestFlow>
</transport>
</deployment>