我可以让MOXy在生成json时重命名元素吗



从通用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重命名一个元素?

您可以使用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" ]
}

有关更多信息

  • 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

最新更新