gpt4 book ai didi

java - 序列化为 XML 时忽略父类

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:28:11 25 4
gpt4 key购买 nike

当子类列表中有 @XmlElement 时,是否有 JAXB 注释来忽略父类?

澄清一下——我想知道是否有比将所有父类 getter/setter 标记为 transient ,然后必须返回到子类并添加 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<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,并且只使用我放入的 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);
}
}

输出

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

关于java - 序列化为 XML 时忽略父类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3728341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com