ignore field based on condition while marshalling

1.1k Views Asked by At

The following class needs to be marshalled to xml.

class A {
    @XmlElement
    private boolean readFlag;

    // ... and some other fields
}

However, the readFlag should only be in the returned xml, under some conditions. I can not use @XmlTransient, because that would always leave it out.

Thanks in advance.

1

There are 1 best solutions below

0
Hein Blöd On BEST ANSWER

One thing you could do is use the Boolean wrapper class and set the value of the field to null if you don't want to have it marshalled - null fields are left out by default.

If you use EclipseLink/MOXy as JAXB implementation, you could also use its object graph feature, for a list of articles about that see here.