1
这是根据例如我的代码: 我一直在寻找格式化地图 里面的日期字段,但它引发此异常:JAXB日食MOXY可空的元素和XmlAdapter与地图<String,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 后,但我没有发现它是相同的逻辑错误。
我试过,但它不工作我回答上面我的尝试。我试图解决我在先前问题中写出的同一个问题,以获得空属性的空标记。我试过这个:<! - 非常这不产生空节点 - > xml-element>但它不起作用 –
2013-04-16 08:37:24
我相信我会使用相同的方法地图<字符串,字符串>,我会用这种方式解决http://stackoverflow.com/questions/15931235/jaxb-eclipse-moxy-nullable-value-and-retrieve-only-a-specified-key-in- A-MAP/16063219#16063219 – 2013-04-17 15:01:43