序列化为XML时忽略父类

当您在子类的List上有@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(); } } 

 public class SpecificHelper extends GenericHelper { List projects; public SpecificHelper (){ projects=new ArrayList(); } @XmlElement(name = "project") @XmlElementWrapper (name = "projectlist") public List getProjects() {return projects;} public void setProjects(List 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; } } 

儿童class

我们将在子节点上使用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); } } 

产量

   childProperty parentProperty2