我能得到莫西产生JSON时重命名一个元素?(Can I get MOXy to rename an

2019-07-30 09:09发布

从共同的JAXB模型生成的XML可以是这样的形式的

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

因为在JSON我们有数组我们不需要这两种元素,所以通过使用莫西的OXML扩展我可以拼合输出给

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

但由于IPI现指事物的数组,我想它被称为IPISIPI

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

有没有办法让莫西重命名一个元素?

Answer 1:

你可以使用的EclipseLink JAXB(莫西)的外部映射文件来调整映射无论是XML或JSON表示。

虚无主义

下面是一个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

我们可以用莫西的外部映射文件来修改如何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

要指定莫西为您的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" ]
}

欲获得更多信息

  • 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


文章来源: Can I get MOXy to rename an element when generating json?