是否可以配置JAXB上下文(對於unmarshaller)本身,以便它可以實例化具有兩個存取器getField()/ setField()和領域? A類不可修改:它是第三方或生成的。配置JAXB解組器處理具有處理器方法和字段的類
class A {
public int field;
public int getField();
public void setField(int field);
}
標準的JAXBContext實例
JAXBContext jaxbContext = JAXBContext.newInstance(A.class);
將給予以下異常
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "field"
this problem is related to the following location:
at public int com.thecompany.A.getField()
at com.thecompany.A
this problem is related to the following location:
at public int com.thecompany.A.field
at com.thecompany.A
這可能與
@XmlAccessorType(XmlAccessType.FIELD)
但這是註釋的類來解決不是可接受的解決方案
是否有辦法保持類定義不變並且能夠從XML文件讀取它的實例? (?可能不是JAXB)
UPD:發現回答同一個問題:JAX-B - How to map a schema element to an existing Java class
或者,使用http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted作爲這樣的回答:Creating Jaxb classes from xml without schema;漂亮的教程在http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html