How to tell JAXB to ignore parent class properties

I am having the following problems with JAXB: it seems that JAXB parses properties from the deepest child class to the parent, and the child property takes precedence. I would like to somehow change this behavior. In particular:

Class child:

package test.sub; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlTransient; @XmlAccessorType(XmlAccessType.NONE) public class BasicDocument { private String comment; public String getComment() { return comment; } public void setComment(String cost) { this.comment = cost; } } 

Parent class:

 package test; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; import test.sub.BasicDocument; @XmlRootElement(name="Description", namespace="http://www.w3.org/1999/02/22-rdf-syntax-ns#") @XmlAccessorType(XmlAccessType.PROPERTY) class Document extends BasicDocument { private String identifier; @XmlElement(name = "identifier", namespace = "http://purl.org/dc/terms/") public String getIdentifier() { return identifier; } public void setIdentifier(String identifier) { this.identifier = identifier; } @Override @XmlElement(name = "abstract", namespace = "http://purl.org/dc/terms/") public String getComment() { return super.getComment(); } @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); } } 

Marshalling works great:

 Document document = new Document(); document.setIdentifier("12A"); document.setComment("special"); StringWriter w = new StringWriter(); jaxbContext.createMarshaller().marshal(document, new StreamResult(w)); System.out.println(w); 

Output:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns2:Description xmlns:ns2="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/dc/terms/"> <abstract>special</abstract> <identifier>12A</identifier> </ns2:Description> 

But marshalling ignores the property that is in the BasicDocument child class ( t.xml is exactly the XML above):

 JAXBContext jaxbContext = JAXBContext.newInstance(Document.class); Document document = (Document) jaxbContext.createUnmarshaller().unmarshal(Document.class.getResourceAsStream("t.xml")); System.out.println("out: " + document); 

Output:

 out: Document[identifier=12A,comment=<null>] 

Expected:

 out: Document[identifier=12A,comment=special] 

Basically, @XmlAccessorType(XmlAccessType.NONE) on a BasicDocument (see Ignore the parent class when Serializing in XML ) has no effect. We also create package-info.java in the test.sub package (see @XmlTransient on a third-party or external superclass ) as follows:

 @XmlAccessorType(XmlAccessType.NONE) package test.sub; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; 

had no effect. Only @XmlTransient public class BasicDocument worked. Ideally, I would not want to add annotations to the child and control this behavior only through package-info.java . How can i do this?

Tested on JDK 1.6.0_27 and, in addition, with the JAXB 2.2.4-1 runtime in the classpath.

Is this a function or an error?

+4
source share
1 answer

You just need to add the setComment method to the Document class. Without it, it is considered as a write-only property, although the method exists in the parent class.

 public void setComment(String comment) { super.setComment(comment); } 

Full source for Document

 package test; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; import test.sub.BasicDocument; @XmlRootElement(name="Description", namespace="http://www.w3.org/1999/02/22-rdf-syntax-ns#") @XmlAccessorType(XmlAccessType.PROPERTY) class Document extends BasicDocument { private String identifier; @XmlElement(name = "identifier", namespace = "http://purl.org/dc/terms/") public String getIdentifier() { return identifier; } public void setIdentifier(String identifier) { this.identifier = identifier; } @Override @XmlElement(name = "abstract", namespace = "http://purl.org/dc/terms/") public String getComment() { return super.getComment(); } public void setComment(String comment) { super.setComment(comment); } @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); } } 
+2
source

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


All Articles