java xml 序列化_java-序列化为XML时忽略父类
當(dāng)子類列表上有@XmlElement時,是否有一個JAXB注釋可以忽略父類?
只是要澄清一下-我想知道是否有比將所有父類的getter / setter標(biāo)記為瞬態(tài)更好的方法,然后不得不回到子類并添加getter / setter并將它們標(biāo)注為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注釋,則將其擴(kuò)展的任何類,如果我有一個方法getSpecificHelper()返回了所有雇主的列表,并使用XmlElement對其進(jìn)行注釋,則所有這些項都將返回帶有名稱,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);
}
}
輸出量
childProperty
parentProperty2
標(biāo)簽:jaxb,data-binding,xml-serialization,java
來源: https://codeday.me/bug/20191105/1997861.html
總結(jié)
以上是生活随笔為你收集整理的java xml 序列化_java-序列化为XML时忽略父类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电路板上的插头怎么拔下来_空调插头一直不
- 下一篇: asp.net ajax 怎么获取前端u