问题描述
从常见的JAXB模型中生成的xml可以是
From a common JAXB model the xml generated can be of the form
<ipi-list><ipi>1001</ipi><ipi>1002</ipi></ipi-list>
因为在json中我们有阵列我们不需要这两个元素,所以通过使用MOXy的oxml扩展我可以展平输出
because in json we have arrays we dont need both elements, so by using MOXy's oxml extensions I can flatten the output to give
"ipi" : [ "1001", "1002" ],
但因为ipi现在指的是一系列我希望它被称为 ipis 而不是的东西ipi
but because ipi now refers to an array of things I would like it to be called ipis not ipi
"ipis" : [ "1001", "1002" ],
有没有办法让MOXy重命名一个元素?
Is there a way to get MOXy to rename an element ?
推荐答案
您可以使用的外部映射文档,用于调整XML或JSON表示的映射。
You could use EclipseLink JAXB (MOXy)'s external mapping document to tweak the mapping for either the XML or JSON representation.
IPIList
下面是一个带有JAXB注释的域类,它与你问题中的XML表示相匹配:
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<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的外部映射文档来修改列表
属性映射到JSON的方式。
We can use MOXy's external mapping document to modify how the list
property is mapped to 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
的文件,并带有以下条目(请参阅参考资料):
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
演示
以下演示代码显示如何在创建 JAXBContext
时引用外部映射文档。
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<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);
}
}
输出
以下是运行演示代码的输出:
Here is the output from running the demo code:
<?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
这篇关于在生成json时,我可以让MOXy重命名元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!