在CXF中用JAXB数据绑定支持Map类型(转)
引入
在CXF所些的服务端程序,被客户端程序调用时,SOAP中不支持Map(客户端传递Map参数或服务端返回Map数据),否则会如下错误:Marshalling Error: java.util.Map is not known to this context。
原因
CXF中不支持Map
解决方案
通过适配器将数组转换成HashMap的方式支持。
代码实现
1、? 定义Map转换器(code)
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
?
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
?
/**
?* @author TEANA E-mail: mmz06@163.com
?* @version 创建时间:2011-7-1 下午11:21:49
?* @DO????? Map转换器 ???????
?*/
@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 static class MapEntry
??? {
??????? 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;
??????? }
?
??????? private String key;
??????? private Object 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;
??????? }
??? }
?
??? public List<MapEntry> getEntries()
??? {
??????? return entries;
??? }
}
?
?
2、? 定义Map适配器(code)
import java.util.HashMap;
import java.util.Map;
?
import javax.xml.bind.annotation.adapters.XmlAdapter;
?
/**
?* @author TEANA E-mail: mmz06@163.com
?* @version 创建时间:2011-7-1 下午11:25:11
?* @DO?????? Map适配器???????
?*/
?
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;
??? }
}
3、? 在CXF服务端的接口程序中进行如下定义
??? @WebMethod?
??? @XmlJavaTypeAdapter(MapAdapter.class)??
??? Map<String, String> getMap();
进行以上3个步骤,就可以实现CXF中对Map的支持。