这是我根据示例的代码:我正在寻找格式化 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 帖子进行操作,但我没有找到相同逻辑的错误。