Can I get MOXy to rename an element when generating json?

前端 未结 1 688
被撕碎了的回忆
被撕碎了的回忆 2021-01-25 09:22

From a common JAXB model the xml generated can be of the form

10011002

相关标签:
1条回答
  • 2021-01-25 10:09

    You could use EclipseLink JAXB (MOXy)'s external mapping document to tweak the mapping for either the XML or JSON representation.

    IPIList

    Below is a domain class with JAXB annotations that matches the XML representation from your question:

    package forum11449219;
    
    import java.util.*;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement(name="ipi-list")
    public class IPIList {
    
        private List<String> list = new ArrayList<String>();
    
        @XmlElement(name="ipi")
        public List<String> getList() {
            return list;
        }
    
        public void setList(List<String> list) {
            this.list = list;
        }
    
    }
    

    oxm.xml

    We can use MOXy's external mapping document to modify how the list property is mapped to JSON.

    <?xml version="1.0"?>
    <xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="forum11449219">
        <java-types>
            <java-type name="IPIList">
                <java-attributes>
                    <xml-element java-attribute="list" name="ipis"/>
                </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    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 ):

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

    Demo

    The following demo code shows how to reference the external mapping document when creating a JAXBContext.

    package forum11449219;
    
    import java.util.*;
    import javax.xml.bind.*;
    import org.eclipse.persistence.jaxb.JAXBContextProperties;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            IPIList ipiList = new IPIList();
            ipiList.getList().add("1001");
            ipiList.getList().add("1002");
    
            // XML
            JAXBContext jc = JAXBContext.newInstance(IPIList.class);
            Marshaller xmkMarshaller = jc.createMarshaller();
            xmkMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            xmkMarshaller.marshal(ipiList, System.out);
    
            // JSON
            Map<String, Object> properties = new HashMap<String, Object>(3);
            properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum11449219/oxm.xml");
            properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
            properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
            JAXBContext jsonJC = JAXBContext.newInstance(new Class[] {IPIList.class}, properties);
            Marshaller jsonMarshaller = jsonJC.createMarshaller();
            jsonMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jsonMarshaller.marshal(ipiList, System.out);
        }
    
    }
    

    Output

    Here is the output from running the demo code:

    <?xml version="1.0" encoding="UTF-8"?>
    <ipi-list>
       <ipi>1001</ipi>
       <ipi>1002</ipi>
    </ipi-list>
    {
       "ipis" : [ "1001", "1002" ]
    }
    

    For More Information

    • http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html
    • http://blog.bdoughan.com/2012/04/extending-jaxb-representing-metadata-as.html
    • http://blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html
    0 讨论(0)
提交回复
热议问题