Can I get MOXy to not output an attribute when gen

2019-02-28 23:57发布

问题:

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

回答1:

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 type field as transient.

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum383861">
    <java-types>
        <java-type name="ReleaseGroup">
            <java-attributes>
                <xml-transient java-attribute="type"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

ReleaseGroup

Below is the domain model I'll use for this example. Note how the type property is annotated with @XmlAttribute.

package forum383861;

import javax.xml.bind.annotation.*;

@XmlRootElement(name="release-group")
@XmlAccessorType(XmlAccessType.FIELD)
public class ReleaseGroup {

    @XmlAttribute
    String type;

    String title;

}

jaxb.properties

To specify MOXy as your JAXB provider you need to include a file called jaxb.properties in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Demo

Since the XML and JSON representations are different we'll create separate JAXBContexts for them. For the JSON one we'll leverage MOXy's external mapping file.

package forum383861;

import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        ReleaseGroup rg = new ReleaseGroup();
        rg.type = "Album";
        rg.title = "Fred";

        // XML
        JAXBContext xmlJC = JAXBContext.newInstance(ReleaseGroup.class);
        Marshaller xmlMarshaller = xmlJC.createMarshaller();
        xmlMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        xmlMarshaller.marshal(rg, System.out);

        // JSON
        Map<String, Object> properties = new HashMap<String, Object>(2);
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum383861/oxm.xml");
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        JAXBContext jsonJC = JAXBContext.newInstance(new Class[] {ReleaseGroup.class}, properties);
        Marshaller jsonMarshaller = jsonJC.createMarshaller();
        jsonMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jsonMarshaller.marshal(rg, System.out);
    }

}

Output

Below is the output from running the demo code:

<?xml version="1.0" encoding="UTF-8"?>
<release-group type="Album">
   <title>Fred</title>
</release-group>
{
   "release-group" : {
      "title" : "Fred"
   }
}