Is there a JAXB annotation to ignore a parent class, when you have an @XmlElement on a List of the child classes?
Just to clarify – I was wondering if there was a better way than marking all of the parent classes getters/setters as transient, and then having to go back to the child classes and add getters/setters and annotating those as XmlElements as well
An Example:
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+"]";
}
}
So in this example, if I take out the XmlTransient annotations in GenericHelper, any class that extends it, if I were to have a method getSpecificHelper() that returned a list of all employers, and annotate it with XmlElement, ALL of those items will return with a name, LOE, RoleName, etc. I am looking for a class annotation to go on GenericHelper so I can avoid having to use all of the @XmlTransients individually, and only use the XmlElement notations I have put in the SpecificHelper
How about?
The Parent Class
We will use XmlAccessType.NONE to tell JAXB that only explicitly annotated fields/properties are mapped.
The Child Class
We will use XmlAccessType.PROPERTY on the child. Any properties from the parent class we want to include will need to be overridden and be explicitly annotated. In this example we will bring in parentProperty2 from the Parent class. You will only need to override the getter from the parent class.
Demo Class
Output