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

前端 未结 1 689
被撕碎了的回忆
被撕碎了的回忆 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 list = new ArrayList();
    
        @XmlElement(name="ipi")
        public List getList() {
            return list;
        }
    
        public void setList(List list) {
            this.list = list;
        }
    
    }
    

    oxm.xml

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

    
    
        
            
                
                    
                
            
        
    
    

    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 properties = new HashMap(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:

    
    
       1001
       1002
    
    {
       "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)
提交回复
热议问题