由于Axis支持自动序列化和反序列化JavaBean,实现了Java对象和XML SOAP消息映射,所以将Java的Map对象传递到.NET最简单的方法是实现一个JavaBean数组对象:
File 1:
public class MapEntryVO {
private Object key;
private Object value;
public MapEntryVO() {
}
public MapEntryVO(Object key, Object value) {
this.key = key;
this.value = value;
}
public Object getKey() {
return key;
}
public void setKey(Object value) {
key = value;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}
File 2:
import java.util.*;
public class WebServicesUtils {
public static MapEntryVO[] convertMapToMapEntryVO(Map conv) {
MapEntryVO[] result = new MapEntryVO[conv.size()];
int i = 0;
Iterator iter = conv.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry item = (Map.Entry) iter.next();
result[i++] = new MapEntryVO(item.getKey(),item.getValue());
}
return result;
}
}
File 3:
// Example WebService
public class TestService {
public MapEntryVO[] testMethod() {
java.util.Map value = new java.util.HashMap();
value.put("Key 1","Value 1");
value.put("Key 2","Value 2");
return WebServicesUtils.convertMapToMapEntryVO(value);
}
}
代码完成后,记得在server-config.wsdd(或部署文件deploy.wsdd)中配置JavaBean的序列化,通知Axis你需要自动序列化的对象:
<typeMapping qname="java:MapEntryVOList"
type="java:MapEntryVO[]"
serializer="org.apache.axis.encoding.ser.ArraySerializerFactory"
deserializer="org.apache.axis.encoding.ser.ArrayDeserializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
<typeMapping qname="java:MapEntryVO"
type="java:MapEntryVO"
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
参考:
http://wiki.apache.org/ws/FrontPage/Axis/DotNetInteropMapInfo