java配置JAXB解组器来处理同时具有acessor方法和字段的类
是否可以配置JAXB上下文(用于解组器)本身,以便它可以为同时具有访问器getField()/setField()和字段的类A实例化上下文?A类不可修改:它是第三方或生成的
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;nice教程在http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html提供
# 1 楼答案
JAXB外部绑定允许您在不更改源代码的情况下进行marshall/unmarshall,我相信这正是您所要求的
http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/2.0/tutorial/doc/JAXBUsing4.html