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

Java 我可以让MOXy在生成json时重命名元素吗?,java,json,jaxb,eclipselink,moxy,Java,Json,Jaxb,Eclipselink,Moxy,从公共JAXB模型生成的xml可以是 <ipi-list><ipi>1001</ipi><ipi>1002</ipi></ipi-list> 但由于ipi现在指的是一系列事物,我希望它被称为ipi而不是ipi "ipis" : [ "1001", "1002" ], 有没有办法让MOXy重命名元素?您可以使用的外部映射文档调整XML或JSON表示的映射 IPIList 下面是一个带有JAXB注释的域类,该注释与您的问题中

从公共JAXB模型生成的xml可以是

<ipi-list><ipi>1001</ipi><ipi>1002</ipi></ipi-list>
但由于ipi现在指的是一系列事物,我希望它被称为ipi而不是ipi

"ipis" : [ "1001", "1002" ],
有没有办法让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;
    }

}
jaxb.properties

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

演示

下面的演示代码显示了如何在创建
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);
    }

}
用于UM11449219的包;
导入java.util.*;
导入javax.xml.bind.*;
导入org.eclipse.persistence.jaxb.JAXBContextProperties;
公开课演示{
公共静态void main(字符串[]args)引发异常{
IPIList IPIList=新IPIList();
ipiList.getList().add(“1001”);
ipiList.getList().add(“1002”);
//XML
JAXBContext jc=JAXBContext.newInstance(IPIList.class);
Marshaller xmkMarshaller=jc.createMarshaller();
setProperty(Marshaller.JAXB_格式化的_输出,true);
xmkMarshaller.marshal(ipiList,System.out);
//JSON
映射属性=新的HashMap(3);
put(JAXBContextProperties.OXM_元数据_源代码,“forum11449219/OXM.xml”);
put(JAXBContextProperties.MEDIA_类型,“application/json”);
put(JAXBContextProperties.JSON_INCLUDE_ROOT,false);
JAXBContext jsonJC=JAXBContext.newInstance(新类[]{IPIList.Class},属性);
Marshaller jsonMarshaller=jsonJC.createMarshaller();
setProperty(Marshaller.JAXB_格式化的_输出,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" ]
}

1001
1002
{
“ipis”:[“1001”、“1002”]
}
了解更多信息

您可以使用的外部映射文档调整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;
    }

}
jaxb.properties

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

演示

下面的演示代码显示了如何在创建
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);
    }

}
用于UM11449219的包;
导入java.util.*;
导入javax.xml.bind.*;
导入org.eclipse.persistence.jaxb.JAXBContextProperties;
公开课演示{
公共静态void main(字符串[]args)引发异常{
IPIList IPIList=新IPIList();
ipiList.getList().add(“1001”);
ipiList.getList().add(“1002”);
//XML
JAXBContext jc=JAXBContext.newInstance(IPIList.class);
Marshaller xmkMarshaller=jc.createMarshaller();
setProperty(Marshaller.JAXB_格式化的_输出,true);
xmkMarshaller.marshal(ipiList,System.out);
//JSON
映射属性=新的HashMap(3);
put(JAXBContextProperties.OXM_元数据_源代码,“forum11449219/OXM.xml”);
put(JAXBContextProperties.MEDIA_类型,“application/json”);
put(JAXBContextProperties.JSON_INCLUDE_ROOT,false);
JAXBContext jsonJC=JAXBContext.newInstance(新类[]{IPIList.Class},属性);
Marshaller jsonMarshaller=jsonJC.createMarshaller();
setProperty(Marshaller.JAXB_格式化的_输出,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" ]
}

1001
1002
{
“ipis”:[“1001”、“1002”]
}
了解更多信息