I have read through numerous posts about custom serialization options using moxy (see Jaxb marshalling with custom annotations as an example) but I cannot seem to find an easy way to exclude a field by default but include it when some property is set.
For example, if I have a simple POJO with three fields
@XmlAccessorType(XmlAccessType.FIELD)
public class Person{
@XmlElement private String email;
@XmlElement private String fullName;
@XmlElement private String ssn;
}
...and I want to leave ssn out by default when serialized, but include it only when I set a specific marshaller property (or the like). How would I go about doing that? In other words, disable the field by default, but include it when I explicitly ask for it. Even if I take the explicit @XmlNamedObjectGraph approach as mentioned in one of the answers in the aforementioned post, I am left with the default option being an object graph with all fields.
Thanks in advance for the help!
If you specify an object graph name when you create the JAXBContext
it will be set by default on all the Marshaller
and Unmarshaller
instances created from it.
Map<String, Object> properties = new HashMap<String, Object>(1);
properties.put(JAXBContextProperties.OBJECT_GRAPH, "yourGraphName");
JAXBContext jc = JAXBContext.newInstance(new Class[] {Foo.class}, properties);