定制网站系统开发,足球网站界面设计,图形设计网站,汶上县建设局官方网站当子类列表上有XmlElement时,是否有一个JAXB注释可以忽略父类#xff1f;只是要澄清一下-我想知道是否有比将所有父类的getter / setter标记为瞬态更好的方法,然后不得不回到子类并添加getter / setter并将它们标注为XmlElements一个例子#xff1a;public class GenericHelp…当子类列表上有XmlElement时,是否有一个JAXB注释可以忽略父类只是要澄清一下-我想知道是否有比将所有父类的getter / setter标记为瞬态更好的方法,然后不得不回到子类并添加getter / setter并将它们标注为XmlElements一个例子public class GenericHelper {String name;String dates;String roleName;String loe;XmlTransientpublic String getName() {return name;}public void setName(String name) {this.name name;}XmlTransientpublic String getDates() {return dates;}public void setDates(String dates) {this.dates dates;}XmlTransientpublic String getRoleName() {return roleName;}public void setRoleName(String roleName) {this.roleName roleName;}XmlTransientpublic String getLOE() {return loe;}public void setLOE(String loe) {this.loe loe.replace(%, ).trim();}}和public class SpecificHelper extends GenericHelper {List projects;public SpecificHelper (){projectsnew ArrayList();}XmlElement(name project)XmlElementWrapper (name projectlist)public List getProjects() {return projects;}public void setProjects(List projects) {this.projects projects;}XmlElementpublic String getName(){return super.getName();}Overridepublic String toString(){String retSpecificHelper [;retname:name;;retdates:dates;;retroleName:roleName;;retloe: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;}}儿童班我们将在子级上使用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;XmlRootElementXmlAccessorType(XmlAccessType.PROPERTY)public class Child extends Parent {private String childProperty;OverrideXmlElementpublic 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);}}输出量childPropertyparentProperty2标签jaxb,data-binding,xml-serialization,java来源 https://codeday.me/bug/20191105/1997861.html