当子类列表上有 @XmlElement 时,是否有 JAXB 注释可以忽略父类?
只是为了澄清 - 我想知道是否有更好的方法,而不是将所有父类 getter/setter 标记为瞬态,然后必须返回子类并添加 getter/setter 并将它们注释为 XmlElements
一个例子:
public class GenericHelper {
String name="";
String dates="";
String roleName="";
String loe="";
@XmlTransient
public String getName() {return name;}
public void setName(String name) {this.name = name;}
@XmlTransient
public String getDates() {return dates;}
public void setDates(String dates) {this.dates = dates;}
@XmlTransient
public String getRoleName() {return roleName;}
public void setRoleName(String roleName) {this.roleName = roleName;}
@XmlTransient
public String getLOE() {return loe;}
public void setLOE(String loe) {
this.loe = loe.replace("%", "").trim();
}
}
and
public class SpecificHelper extends GenericHelper {
List<ProjectHelper> projects;
public SpecificHelper (){
projects=new ArrayList<ProjectHelper>();
}
@XmlElement(name = "project")
@XmlElementWrapper (name = "projectlist")
public List<ProjectHelper> getProjects() {return projects;}
public void setProjects(List<ProjectHelper> projects) {this.projects = projects;}
@XmlElement
public String getName(){
return super.getName();
}
@Override
public String toString(){
String ret="SpecificHelper [";
ret+="name:"+name+";";
ret+="dates:"+dates+";";
ret+="roleName:"+roleName+";";
ret+="loe:"+loe+";";
ret+="\n\tprojects:"+projects+";";
return ret+"]";
}
}
因此,在这个示例中,如果我取出 GenericHelper 中的 XmlTransient 注释以及扩展它的任何类,如果我有一个方法 getSpecificHelper() 返回所有雇主的列表,并使用 XmlElement 对其进行注释,那么所有这些项目都将返回名称、LOE、RoleName 等。我正在寻找一个用于 GenericHelper 的类注释,这样我就可以避免必须单独使用所有 @XmlTransients,并且仅使用我在 SpecificHelper 中放入的 XmlElement 表示法
怎么样?
家长班
我们将使用 XmlAccessType.NONE 告诉 JAXB 仅映射显式注释的字段/属性。
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
@XmlAccessorType(XmlAccessType.NONE)
public class Parent {
private String parentProperty1;
private String parentProperty2;
public String getParentProperty1() {
return parentProperty1;
}
public void setParentProperty1(String parentProperty1) {
this.parentProperty1 = parentProperty1;
}
public String getParentProperty2() {
return parentProperty2;
}
public void setParentProperty2(String parentProperty2) {
this.parentProperty2 = parentProperty2;
}
}
儿童班
我们将在子项上使用 XmlAccessType.PROPERTY。我们想要包含的父类的任何属性都需要被覆盖并显式注释。在此示例中,我们将从 Parent 类引入parentProperty2。您只需要重写父类的 getter 方法。
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Child extends Parent {
private String childProperty;
@Override
@XmlElement
public String getParentProperty2() {
return super.getParentProperty2();
}
public String getChildProperty() {
return childProperty;
}
public void setChildProperty(String childProperty) {
this.childProperty = childProperty;
}
}
示范课
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Child.class);
Child child = new Child();
child.setParentProperty1("parentProperty1");
child.setParentProperty2("parentProperty2");
child.setChildProperty("childProperty");
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(child, System.out);
}
}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<child>
<childProperty>childProperty</childProperty>
<parentProperty2>parentProperty2</parentProperty2>
</child>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)