How to ignore annotated JAXB properties in parent class?

We have a class with a JAXB annotation on a single property. Then we have several subclasses that annotate the rest of the important data. However, we have one subclass where we want to ignore the annotation of the parent class so that it does not sort. Here is a sample code.

Parent class:

@XmlType(name="Request") @XmlAccessorType(XmlAccessorType.NONE) public abstract class Request { @XmlElement(required=true) private UUID uuid; ... setter/getter } 

Now for the subclass:

 @Xsd(name="concreteRequest") @XmlRootElement("ConcreteRequest") @XmlType(name="ConcreteRequest") @XmlAccessorType(XmlAccessorType.FIELD) public class ConcreteClass { @XmlElement(required=true) private String data1; @XmlElement(required=true) private String data1; ... setters/getters ... } 

When I start an instance of ConcreteClass, I get the following XML:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ConcreteRequest> <uuid>uuid</uuid> <data1>data</data1> <data2>data</data3> </ConcreteRequest> 

Where I want the XML as follows:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ConcreteRequest> <data1>data</data1> <data2>data</data3> </ConcreteRequest> 

We have other implementations of the request, but this requires a UUID, this is just a special case. Is there a way to ignore the UUID field in my ConcreteRequest?

+8
marshalling jaxb
source share
2 answers

I hope I understand your problem. Here is the solution.

JAXB provides @XmlTransient(javax.xml.bind.annotation.XmlTransient) (javadoc) to ignore any field during sorting.

Redefine the uuid field as @XmlTransient in the derived class (ConcreteRequest.class) along with its Getter / Setter correlation method. You must also override the Getter / Setter methods, they will be called during sorting.

 @Xsd(name="concreteRequest") @XmlRootElement("ConcreteRequest") @XmlType(name="ConcreteRequest") @XmlAccessorType(XmlAccessorType.FIELD) public class ConcreteClass { @XmlElement(required=true) private String data1; @XmlElement(required=true) private String data2; @XmlTransient private UUID uuid; ... setters/getters ... } 

This will override your base class attribute.

Come back to me for more information.

+6
source share

you can use

 @XmlAccessorType(XmlAccessType.NONE) 

in the parent class and

 @XmlAccessorType(XmlAccessType.FIELD) 

for a derived class.

0
source share

All Articles