XmlAdapterをHashMapに適用しようとしていますが、例外が発生しています。私はthis blog entryに非常によく従っていました。私は何度も自分のコードを読んできましたが、私はその問題を見ていません。XmlAdapterを正常に動作させることができません
最新バージョンのorg.eclipse.persistence.jaxb.JAXBContextFactory
をJAXBプロバイダとして使用しています。
<test>
<myName>Paul</myName>
<mappings>
<entry key="man">manufacturer</entry>
<entry key="prod">product</entry>
</mappings>
<test>
上記のブログの記事で手順を実行します:
1.私がしようとしているマップできないクラス
を識別ここ
は私のXMLのサンプルです地図はjava.util.HashMap
です。
2. XmlAdapterのを指定
public class MappingAdapter extends XmlAdapter<MappingType,
HashMap<String, String>>
{
@Override
public HashMap<String, String> unmarshal(MappingType v> throws Exception
{
HashMap<String, String> hashMap = new HashMap<String, String>();
for (MappingTypeEntry mappingEntry : v.entry)
{
hashMap.put(mappingEntry.key, mappingEntry.value);
}
return hashMap;
}
// marshal is here but I'm just working on unmarshalling now
}
4.オブジェクトのマッピング可能
public class MappingType
{
public List<MappingEntryType> entry = new ArrayList<MappingEntryType>();
}
public class MappingEntryType
{
@XmlAttribute
public String key;
@XmlValue
public String value;
}
3.マップできないとのマッピング可能な間の変換XmlAdapterの作成された等価クラスを作成します。
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "test")
public class TestEntity
{
@XmlElement
private String myName;
@XmlJavaTypeAdapter(MappingAdapter.class)
HashMap<String, String> mappings;
// getters & setters omitted in a feeble attempt at brevity
}
私は 5.スタックトレース
Exception [EclipseLink-3001](Eclipse Persistence Services-2.3.0.v20110604-r9504):
org.eclipse.persistence.exceptions.ConversionException
ExceptionDescription: The object [[email protected]], of class
[class mypackage.MappingType],could not be converted to [class java.util.HashMap]
at etc etc
例外の説明は非常に明確であるが、私が見ることができないが、私はどこにMappingType
を変換しようとしています呼び出して次のステップを、追加しましたHashMap
。時には質問を入力すると、私は答えにつながりますが、今回はありません。
私はそれが何か簡単だと確信しています - 私の間違いを見たら、それを指摘してください!
ありがとうございます!
ちなみに、Blaise Doughan's blogはJAXBとMOXyの素晴らしい情報が満載で、チェックアウトする価値があります。