【问题标题】:Can I get MOXy to rename an element when generating json?我可以在生成 json 时让 MOXy 重命名元素吗?
【发布时间】:2012-07-12 01:10:25
【问题描述】:

从一个常见的 JAXB 模型生成的 xml 可以是以下形式

<ipi-list><ipi>1001</ipi><ipi>1002</ipi></ipi-list>

因为在 json 中我们有数组,我们不需要两个元素,所以通过使用 MOXy 的 oxml 扩展,我可以展平输出以给出

"ipi" : [ "1001", "1002" ],

但因为 ipi 现在指的是一系列事物,我希望将其称为 ipis 而不是 ipi

"ipis" : [ "1001", "1002" ],

有没有办法让 MOXy 重命名元素?

【问题讨论】:

    标签: java json jaxb eclipselink moxy


    【解决方案1】:

    您可以使用EclipseLink JAXB (MOXy) 的外部映射文档来调整 XML 或 JSON 表示的映射。

    IPIList

    下面是一个带有 JAXB 注释的域类,它与您问题中的 XML 表示相匹配:

    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

    我们可以使用 MOXy 的外部映射文档来修改 list 属性如何映射到 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

    要将 MOXy 指定为您的 JAXB 提供程序,您需要在与您的域模型相同的包中包含一个名为 jaxb.properties 的文件,其中包含以下条目(请参阅 ):

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

    演示

    以下演示代码展示了在创建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);
        }
    
    }
    

    输出

    这是运行演示代码的输出:

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

    更多信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-16
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多