JAXB用于将java对象编组到文件中。
@XmlRootElement
public class CompleteCollectionInfo {
@XmlElement(required = true)
@XmlID
@XmlSchemaType(name = "ID")
protected String uid;
}
@XmlRootElement
public class AssociationInfo {
@XmlElement(required = true)
@XmlIDREF
@XmlSchemaType(name = "IDREF")
@XmlJavaTypeAdapter(type= CompleteCollectionInfo.class , value =AssociationCompleteCollectionInfoAdapter.class)
protected CompleteCollectionInfo associatedcollection;
}
适配器将确保在关联信息中正确设置之前已解组的所有CompleteCollectionInfo引用
public class AssociationCompleteCollectionInfoAdapter extends XmlAdapter<String, CompleteCollectionInfo>{
@Override
public CompleteCollectionInfo unmarshal(String v) throws Exception {
CompleteCollectionInfo completeCollectionInfo = UnBlobUtil.completeCollectionCache.get(v);
if (completeCollectionInfo == null )
completeCollectionInfo = new CompleteCollectionInfo();
completeCollectionInfo.setUid(v);
return completeCollectionInfo;
}
@Override
public String marshal(CompleteCollectionInfo v) throws Exception {
return v.getUid();
}
}
编组时遇到的异常: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:IllegalAnnotationExceptions的1个计数 XmlIDREF属性引用了一个没有XmlID属性的类型“java.lang.String”。
答案 0 :(得分:1)
您的XmlAdapter
告诉JAXB
将CompleteCollectionInfo
视为String
。所以String
确实没有用@XmlID
注释的属性。 @XmlID
/ @XmlIDREF
用于文档中的引用,因此无论如何它都不适用于您的用例。
您已经使用XmlAdapter
为您的用例做了正确的事情。为了让事情顺利进行,您只需要删除@XmlID
/ @XmlIDREF
。
答案 1 :(得分:0)
为了使JAXBContext
知道如何解组Student
,您还必须执行以下操作:
JAXBContext theContext = JAXBContext.newInstance (Group.class, Student.class);