我正在尝试通过 SOAP 传递 Hashmap。我正在使用 CXF wsdl2java 创建我的模式。我已经为我的 HashMap 创建了一个包装类,因为 Hashmap 本身不能通过线路传递。
然后,我创建了适配器以将该 Hashmap 变形为我的 wsdl 的已知类型,但是当我的 wsdl 创建时,它添加了一些不需要的抽象映射。下面是代码:
这是我的 HashMap 包装类
@XmlRootElement(name = "testTO")
public class TestTO {
private HashMap<String, Object> mapTest;
public TestTO(){
this.mapTest = new HashMap<String, Object>();
}
@XmlJavaTypeAdapter(MapAdapter.class)
public HashMap<String, Object> getMapTest() {
return mapTest;
}
public void setMapTest(HashMap<String, Object> mapTest) {
this.mapTest = mapTest;
}
}
这是 MyMap 类,其中是已知的模式类型
@XmlJavaTypeAdapter(MapAdapter.class)
public class MyMap extends HashMap<String, Object>{
public final List<Entry> entryList = new ArrayList<Entry>();
}
这是该列表包含上面的条目类:
public class Entry {
@XmlAttribute
public String key;
@XmlElements({
@XmlElement(name = "byte", type = byte.class),
@XmlElement(name = "short", type = short.class),
@XmlElement(name = "int", type = int.class),
@XmlElement(name = "long", type = long.class),
@XmlElement(name = "float", type = float.class),
@XmlElement(name = "double", type = double.class),
@XmlElement(name = "char", type = char.class),
@XmlElement(name = "boolean", type = boolean.class),
@XmlElement(name = "ByteWrapper", type = Byte.class),
@XmlElement(name = "ShortWrapper", type = Short.class),
@XmlElement(name = "IntegerWrapper", type = Integer.class),
@XmlElement(name = "LongWrapper", type = Long.class),
@XmlElement(name = "FloatWrapper", type = Float.class),
@XmlElement(name = "DoubleWrapper", type = Double.class),
@XmlElement(name = "Character", type = Character.class),
@XmlElement(name = "BooleanWrapper", type = Boolean.class),
@XmlElement(name = "BigDecimal", type = BigDecimal.class),
@XmlElement(name = "String", type = String.class),
@XmlElement(name = "Date", type = Date.class)
})
public Object value;
public Entry() {
this.key = null;
this.value = null;
}
public Entry(String key, Object value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public Object getValue() {
return value;
}
}
这是我的适配器:
public class MapAdapter extends XmlAdapter<MyMap, Map<String, Object>> {
@Override
public MyMap marshal(Map<String, Object> v) throws Exception {
MyMap myMap = new MyMap();
for ( Map.Entry<String, Object> e : v.entrySet() ) {
Entry entry = new Entry();
entry.key = e.getKey();
entry.value = e.getValue();
myMap.entryList.add(entry);
}
return myMap;
}
@Override
public Map<String, Object> unmarshal(MyMap v) throws Exception {
Map<String, Object> map = new HashMap<String,Object>();
for ( Entry e : v.entryList ) {
map.put(e.key, e.value);
}
return map;
}
}
但我的 wsdl 正在生成以下内容:
<xs:element minOccurs="0" name="foo" type="tns:testTO"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="testTO">
<xs:sequence>
<xs:element minOccurs="0" name="mapTest" type="tns:myMap"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="myMap">
<xs:complexContent>
<xs:extension base="tns:hashMap">
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="entryList" nillable="true" type="tns:entry"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="hashMap">
<xs:complexContent>
<xs:extension base="tns:abstractMap">
<xs:sequence/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType abstract="true" name="abstractMap">
<xs:sequence/>
</xs:complexType>
<xs:complexType name="entry">
<xs:sequence>
<xs:choice minOccurs="0">
<xs:element name="byte" type="xs:byte"/>
<xs:element name="short" type="xs:short"/>
<xs:element name="int" type="xs:int"/>
<xs:element name="long" type="xs:long"/>
<xs:element name="float" type="xs:float"/>
<xs:element name="double" type="xs:double"/>
<xs:element name="char" type="xs:unsignedShort"/>
<xs:element name="boolean" type="xs:boolean"/>
<xs:element name="ByteWrapper" type="xs:byte"/>
<xs:element name="ShortWrapper" type="xs:short"/>
<xs:element name="IntegerWrapper" type="xs:int"/>
<xs:element name="LongWrapper" type="xs:long"/>
<xs:element name="FloatWrapper" type="xs:float"/>
<xs:element name="DoubleWrapper" type="xs:double"/>
<xs:element name="Character" type="xs:unsignedShort"/>
<xs:element name="BooleanWrapper" type="xs:boolean"/>
<xs:element name="BigDecimal" type="xs:decimal"/>
<xs:element name="String" type="xs:string"/>
<xs:element name="Date" type="xs:dateTime"/>
</xs:choice>
</xs:sequence>
<xs:attribute name="key" type="xs:string"/>
</xs:complexType>
我查看了我在这里找到的多个其他案例,但没有一个能够解决我的问题,我什至参考了http://docs.oracle.com/javase/6/docs/api/javax/xml/bind/ annotation/adapters/XmlAdapter.html 但 wsdl 到 java 似乎正在搞乱架构。
谢谢。