根据示例,这是我的代码: 我一直在寻找格式化Map中的Date字段 但它抛出了这个例外:
The object [{birthDate=1963-07-16 00:00:00.0}], of class [class org.hibernate.collection.internal.PersistentMap], could not be converted to [class it.cineca.jaxb.adapter.MyMapType].
实施例
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public final class MyMapAdapter extends
XmlAdapter<MyMapType,Map<String, Date>> {
private SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
@Override
public MyMapType marshal(Map<String, Date> arg0) throws Exception {
MyMapType myMapType = new MyMapType();
for(Entry<String, Date> entry : arg0.entrySet()) {
MyMapEntryType myMapEntryType =
new MyMapEntryType();
myMapEntryType.key = entry.getKey();
myMapEntryType.value = dateFormat.parse(entry.getValue().toString());
myMapEntryType.value = entry.getValue();
myMapType.entry.add(myMapEntryType);
}
return myMapType;
}
@Override
public Map<String, Date> unmarshal(MyMapType arg0) throws Exception {
HashMap<String, Date> hashMap = new HashMap<String, Date>();
for(MyMapEntryType myEntryType : arg0.entry) {
hashMap.put(myEntryType.key, myEntryType.value);
}
return hashMap;
}
}
import java.util.Date;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
public class MyMapEntryType {
@XmlAttribute
public String key;
@XmlValue
public Date value;
}
import java.util.ArrayList;
import java.util.List;
public class MyMapType {
public List<MyMapEntryType> entry =
new ArrayList<MyMapEntryType>();
}
这是人物绑定文件
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="it.model" xml-mapping-metadata-complete="true">
<xml-schema
element-form-default="QUALIFIED"/>
<java-types>
<java-type name="Person" xml-accessor-type="NONE">
<xml-root-element/>
<xml-type prop-order="firstName lastName stringMap "/>
<java-attributes>
nillable="true"/>
<xml-element java-attribute="stringMap" name="string-map" nillable="true"/>
<xml-element java-attribute="dateMap" name="date-map" nillable="true">
<xml-java-type-adapter value="it.cineca.jaxb.adapter.MyMapAdapter" />
</xml-element>
<xml-element java-attribute="positionCurrentSet" name="position-current-set" nillable="true">
<!-- UNLIKELY THIS DOESN'T PRODUCE EMPTY NODE -->
<xml-null-policy xsi-nil-represents-null="true" empty-node-represents-null="true" null-representation-for-xml="EMPTY_NODE" is-set-performed-for-absent-node="true" />
</xml-element>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
那我怎么解决呢? 我试过跟http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html 发布,但我没有发现错误,这是相同的逻辑。
答案 0 :(得分:2)
我认为你在MyMapAdapter中反转了参数类型。来自JAXB javadocs:
公共抽象类XmlAdapter&lt; ValueType,BoundType&gt;
BoundType - JAXB不知道如何处理的类型。编写适配器以允许此类型通过ValueType用作内存中表示。
ValueType - JAXB知道如何开箱即用的类型。
尝试将您的班级签名更改为MyMapAdapter extends XmlAdapter<Map<String, Date>, MyMapType>
,并交换您的编组和解组方法。
希望这有帮助,
瑞克