Jaxb-eclipse-moxy-Nullable-in-element和XmlAdapter-with-Map<;字符串,日期>;

Jaxb-eclipse-moxy-Nullable-in-element和XmlAdapter-with-Map<;字符串,日期>;,jaxb,moxy,Jaxb,Moxy,这是我根据示例编写的代码: 我想在地图中设置日期字段的格式 但它抛出了一个例外: 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.Simple

这是我根据示例编写的代码: 我想在地图中设置日期字段的格式 但它抛出了一个例外:

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>();

}
import java.text.simpleDataFormat;
导入java.util.Date;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.Map.Entry;
导入javax.xml.bind.annotation.adapters.XmlAdapter;
公共最终类MyMapAdapter扩展
XmlAdapter{
私有SimpleDataFormat dateFormat=新SimpleDataFormat(“dd/MM/yyyy”);
@凌驾
公共MyMapType封送处理程序(映射arg0)引发异常{
MyMapType MyMapType=新的MyMapType();
对于(条目:arg0.entrySet()){
MyMapEntryType MyMapEntryType=
新建MyMapEntryType();
myMapEntryType.key=entry.getKey();
myMapEntryType.value=dateFormat.parse(entry.getValue().toString());
myMapEntryType.value=entry.getValue();
myMapType.entry.add(myMapEntryType);
}
返回myMapType;
}
@凌驾
公共映射解组器(MyMapType arg0)引发异常{
HashMap HashMap=新的HashMap();
对于(MyMapEntryType myEntryType:arg0.entry){
put(myEntryType.key,myEntryType.value);
}
返回hashMap;
}
}
导入java.util.Date;
导入javax.xml.bind.annotation.XmlAttribute;
导入javax.xml.bind.annotation.XmlValue;
公共类MyMapEntryType{
@XmlAttribute
公共字符串密钥;
@XmlValue
公共日期值;
}
导入java.util.ArrayList;
导入java.util.List;
公共类MyMapType{
公共列表条目=
新的ArrayList();
}
这是个人绑定文件

     <?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>

nillable=“true”/>
那我怎么解决呢? 我试着跟着
post,但我没有发现错误,这是相同的逻辑。

我认为MyMapAdapter中的参数类型颠倒了。从jaxbjavadocs:

公共抽象类XmlAdapter

BoundType—JAXB不知道如何处理的类型。编写适配器以允许通过ValueType将此类型用作内存中的表示形式

ValueType—JAXB知道如何开箱即用地处理的类型

尝试将类签名更改为MyMapAdapter Extendes XmlAdapter,并交换封送和解封送方法

希望这有帮助


瑞克

我试过了,但没用,我用上面的尝试回答了。我试图解决我在前一个问题中写的相同问题,为null属性获取空标记。我试过这样做:但它不起作用。我相信我会对地图使用相同的方法,我会用这种方法解决