バックグラウンド
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のアクセス タイプを使用することであることを理解しています。
XmlTransient によって注釈が付けられていない限り、すべての public getter/setter ペアとすべての public フィールドは自動的に XML にバインドされます。
質問
それで、私の質問は、フィールドを無視してゲッター/セッターだけを使用するように JAXB に指示するにはどうすればよいかということです。無視する必要があるプライベート フィールドがいくつかあるため、この方法を好むことに注意してください (実際には、Attachment が誤ってパブリックに設定されていると思います)。