背景
我在一个名为 Attachment 的 JAR 文件中有一个类。定义的重要部分如下。
public class Attachment
{
public List<Media> media;
public List<Media> getMedia()
{
return this.media;
}
public void setMedia(List<Media> media)
{
this.media = media;
}
}
我正在尝试使用JAXB-impl 2.1.3使用以下代码对其进行反序列化:
JAXBContext jaxbContext = JAXBContext.newInstance(Attachment.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(text);
Attachment attachment = (Attachment) unmarshaller.unmarshal(reader);
但是,这给了我以下错误(为简洁起见)
com.sun.xml.bind.v2.runtime.IllegalAnnotationsException:
1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "media"
this problem is related to the following location:
at public java.util.List com.thirdparty.Attachment.getMedia()
...
this problem is related to the following location:
at public java.util.List com.thirdparty.Attachment.media
...
我知道问题在于,默认情况下,JAXB 将使用PUBLIC_MEMBER的访问类型,其定义为:
每个公共 getter/setter 对和每个公共字段都将自动绑定到 XML,除非由 XmlTransient 注释。
问题
所以,我的问题是,我如何告诉 JAXB 忽略该字段而只使用 getter/setter。请注意,我更喜欢这种方式,因为我需要忽略许多私有字段(我相信实际上附件已被错误地设置为公共)。