java xmlmaphandle,java.util.Map is an interface, and JAXB can't handle interfaces 解决方法,java.util...

java.util.Map is an interface, and JAXB can't handle interfaces 解决方法,java.util.mapjaxb,如果在类中定义了接口类型

如果在类中定义了接口类型,然后去序列化时会出现标题的异常,堆栈如下:Exception in thread "main" javax.xml.bind.DataBindingException: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 3 counts of IllegalAnnotationExceptionsJAXB annotation is placed on a method that is not a JAXB property this problem is related to the following location: at @javax.xml.bind.annotation.XmlTransient() at cn.outofmemory.stable.STableMgrjava.util.Map is an interface, and JAXB can't handle interfaces. this problem is related to the following location: at java.util.Map at public java.util.Map cn.outofmemory.stable.STableMgr.getsTables() at cn.outofmemory.stable.STableMgrjava.util.Map does not have a no-arg default constructor. this problem is related to the following location: at java.util.Map at public java.util.Map cn.outofmemory.stable.STableMgr.getsTables() at cn.outofmemory.stable.STableMgr at javax.xml.bind.JAXB._marshal(JAXB.java:549) at javax.xml.bind.JAXB.marshal(JAXB.java:307) at cn.outofmemory.stable.STableMgr.main(STableMgr.java:53)Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 3 counts of IllegalAnnotationExceptions.....

要解决这个问题需要自定义XmlAdapter,假定我们要序列化的类如下,该类有一个Map的属性。package cn.outofmemory.stable;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.xml.bind.JAXB;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;import javax.xml.bind.annotation.XmlTransient;import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;import cn.outofmemory.pojo.Table;@XmlRootElementpublic class STableMgr { private Map> sTables; @XmlElement(name="sTableMap") @XmlJavaTypeAdapter(MapAdapter.class) public Map> getsTables() { return sTables; } public void setsTables(Map> sTables) { this.sTables = sTables; } public boolean hasMTable(String srcTableName) { if (this.sTables == null) { return false; } String lowerCase = srcTableName.toLowerCase(); return this.sTables.containsKey(lowerCase); } public List

需要自己实现的Map接口的XmlAdapter实现如下:package cn.outofmemory.stable;import java.util.ArrayList;import java.util.HashMap;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.XmlElement;import javax.xml.bind.annotation.XmlElementWrapper;import javax.xml.bind.annotation.XmlTransient;import javax.xml.bind.annotation.XmlType;import javax.xml.bind.annotation.adapters.XmlAdapter;import cn.outofmemory.pojo.Table;public class MapAdapter extends XmlAdapter>> { @XmlType(name = "Converter") @XmlAccessorType(XmlAccessType.FIELD) public static class Converter { @XmlTransient private List entries = new ArrayList(); public void addEntry(MapEntry entry) { entries.add(entry); } // @XmlElementWrapper(name = "tableMaps") @XmlElement(name = "entry") public List getEntries() { return entries; } public static class MapEntry { private String key; private List

通过自定义的MapAdapter就可以正常的使用jaxb序列化Map接口类型了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值