方法一:
类型转换类及适配器类
public class MapAdapter extends XmlAdapter<MapConvertor, Map<String, Object>> {
@Override
public MapConvertor marshal(Map<String, Object> map) throws Exception {
MapConvertor convertor = new MapConvertor();
for (Map.Entry<String, Object> entry : map.entrySet()) {
MapConvertor.MapEntry e = new MapConvertor.MapEntry(entry);
convertor.addEntry(e);
}
return convertor;
}
@Override
public Map<String, Object> unmarshal(MapConvertor map) throws Exception {
Map<String, Object> result = new HashMap<String, Object>();
for (MapConvertor.MapEntry e : map.getEntries()) {
result.put(e.getKey(), e.getValue());
}
return result;
}
}
@XmlType(name = "MapConvertor")
@XmlAccessorType(XmlAccessType.FIELD)
public class MapConvertor {
private List<MapEntry> entries = new ArrayList<MapEntry>();
public void addEntry(MapEntry entry) {
entries.add(entry);
}
public List<MapEntry> getEntries() {
return entries;
}
public static class MapEntry {
private String key;
private Object value;
public MapEntry() {
super();
}
public MapEntry(Map.Entry<String, Object> entry) {
super();
this.key = entry.getKey();
this.value = entry.getValue();
}
public MapEntry(String key, Object value) {
super();
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}
}
@WebService
public interface TestMapService {
@XmlJavaTypeAdapter(MapAdapter.class)
public Map<String,Object> search() throws SearchException;
}
这种方式可以处理常规的类型及对象类型
方式二:
@XmlJavaTypeAdapter(MapAdapter.class)是标明该处需要进行转换,转换过的工具方法是MapAdapter.
该类必须是XmlAdapter<ValueType,BoundType>
其中ValueType是转换后Cxf能够支持的对象。
BoundType是需要转换的对象。
转换类: xstream.jar提供的XStream实现对象和String类型及List<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.7</version>
</dependency>
public class MapAdapter extends XmlAdapter<String , Map<String, Object>> {
@Override
public Map<String, Object> unmarshal(String v) throws Exception {
XStream objXStream = new XStream(new DomDriver());
return (Map<String,Object>) objXStream.fromXML(v);
}
@Override
public String marshal(Map<String, Object> v) throws Exception {
XStream objXStream = new XStream();
return objXStream.toXML(v);
}
}