So given I have class Name:
class Name {
private String first;
private String middle;
private String last;
public getFirst() { return first; }
public getMiddle() { return middle; }
public getLast() { return last; }
}
I would like to serialize instances of this class using the mapping XML without having to list each property in the mapping XML:
<java-types>
<java-type name="Name">
<java-attributes>
<xml-element java-attribute="first"/>
<xml-element java-attribute="middle"/>
<xml-element java-attribute="last"/>
</java-attributes>
</java-type>
</java-types>
So ideally I would like to have mapping file like this:
<java-types>
<java-type name="Name" xml-accessor-type="GETTERS"/>
</java-types>
I have some legacy DTO classes like this intended for for serialization only (no setters intentionally) with 30 or more properties and ideally I would want to avoid listing each single property in the mapping file.
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
For this use case I would recommend using the field access type. When this is specified the JAXB implementation will use the fields (instance variables) to access the data instead of going through the property (get method). Below I'll demonstrate how to do this using MOXy's external mapping document extension:
bindings.xml
Name
jaxb.properties
To specify MOXy as your JAXB provider you need to add a file called
jaxb.properties
in the same package as your domain classes with the following entry.Demo
The following code demonstrates how to pass in the bindings file when bootstrapping the
JAXBContext
:Output
For More Information