Ignore parent class when serializing in XML

Is there a JAXB annotation to ignore the parent class when you have @XmlElement in the list of child classes?

Just to clarify - I was wondering if there was a better way than marking all the getters / setters of the parent classes as temporary and then going back to the child classes and adding the getters / setters and annotating them as XmlElements as a

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 post XmlTransient annotations in GenericHelper, any class that extends it, if I had a getSpecificHelper () method that returned a list of all employers and annotated it using XmlElement, ALL of these elements are returned with a name, LOE, RoleName, etc. I'm looking for a class annotation to switch to GenericHelper, so I can avoid using all @XmlTransients separately and only use the XmlElement notations that I entered in SpecificHelper

+3
source share
1 answer

What about?

Parent class

We will use XmlAccessType.NONE to tell JAXB that only explicitly annotated fields / properties are displayed.

 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; } } 

Kids class

We will use XmlAccessType.PROPERTY for the child. Any properties of the parent class that we want to include must be overridden and must be explicitly annotated. In this example, we will quote parentProperty2 from the Parent class. You will only need to override getter from the parent class.

 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; } } 

Demo class

 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); } } 

Exit

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <child> <childProperty>childProperty</childProperty> <parentProperty2>parentProperty2</parentProperty2> </child> 
+4
source

Source: https://habr.com/ru/post/1396524/


All Articles