An instance of my JAXB Object model contains an attribute that I want output when I generate Xml for the instance but not when I generate json
i.e I want
<release-group type="Album">
<title>Fred</title>
</release-group>
and
"release-group" : {
"title" : "fred",
},
but have
"release-group" : {
"type" : "Album",
"title" : "fred"
},
Can I do this using the oxml.xml mapping file
Since your JSON binding is slightly different from your XML binding I would use EclipseLink JAXB (MOXy)‘s external mapping file.
oxm.xml
In the external mapping file we will mark the
typefield as transient.ReleaseGroup
Below is the domain model I’ll use for this example. Note how the
typeproperty is annotated with@XmlAttribute.jaxb.properties
To specify MOXy as your JAXB provider you need to include a file called
jaxb.propertiesin the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).Demo
Since the XML and JSON representations are different we’ll create separate
JAXBContextsfor them. For the JSON one we’ll leverage MOXy’s external mapping file.Output
Below is the output from running the demo code: