我需要编码(marshal) json 和 xml 输出。但是,如何在 Spring 中使用 MOXy JAXB 生成 JSON 输出?我可以生成 xml 文件,如示例所示:http://wiki.eclipse.org/EclipseLink/Examples/MOXy/Spring/JAXBDynamicOXM
文档中没有任何输出json的示例。我知道我可以使用 JAXB Marshaller 通过设置 jaxb 属性来生成 json 输出。如何使用 MOXy/Spring/JAXB 执行相同操作?
感谢您的帮助!
最佳答案
注意:我是EclipseLink JAXB (MOXy) JAXB (JSR-222) 的领导者和成员专家组。
以下答案基于 MOXy wiki 中的示例:
eclipselink-oxm.xml
通常 JAXB/MOXy 与静态域模型一起使用。在您排列的示例中,没有真正的模型,所有元数据都是通过 MOXy 的元数据文件提供的。
<?xml version="1.0" encoding="US-ASCII"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="example.gettingstarted">
<java-types>
<java-type name="Customer">
<xml-root-element/>
<java-attributes>
<xml-element java-attribute="name" type="java.lang.String" xml-path="personal-info/name/text()" />
<xml-element java-attribute="address" type="example.gettingstarted.Address" xml-path="contact-info/address" />
<xml-element java-attribute="phoneNumbers" type="example.gettingstarted.PhoneNumber" container-type="java.util.ArrayList" xml-path="contact-info/phone-number" />
</java-attributes>
</java-type>
<java-type name="PhoneNumber">
<java-attributes>
<xml-attribute java-attribute="type" type="java.lang.String"/>
<xml-value java-attribute="value" type="java.lang.String"/>
</java-attributes>
</java-type>
<java-type name="Address">
<xml-root-element/>
<java-attributes>
<xml-element java-attribute="street" type="java.lang.String" xml-path="street/text()" />
<xml-element java-attribute="city" type="java.lang.String" xml-path="city/text()" />
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
jaxb.properties
要将 MOXy 与动态模型一起使用,您需要指定与正常情况不同的 jaxb.properties 文件:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory
演示
在下面的示例中,我们将把 XML 文档转换为动态模型,然后将其编码(marshal)为 JSON。请注意,上下文路径(本例中为“forum12162216”)必须与包含 jaxb.properties 文件的包名称相对应。通过在 Marshaller
上设置 "eclipselink.media-type"
属性来启用 JSON 绑定(bind)。
package forum12162216;
import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.dynamic.DynamicEntity;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.eclipse.persistence.jaxb.MarshallerProperties;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(1);
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum12162216/eclipselink-oxm.xml");
JAXBContext jc = JAXBContext.newInstance("forum12162216", Demo.class.getClassLoader(), properties);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum12162216/customer.xml");
DynamicEntity customer = (DynamicEntity) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.marshal(customer, System.out);
}
}
customer.xml
<?xml version="1.0" encoding="UTF-8"?>
<customer>
<personal-info><name>Jane Doe</name></personal-info>
<contact-info>
<address>
<city>My Town</city>
<street>123 Any Street</street>
</address>
<phone-number type="work">613-555-1111</phone-number>
<phone-number type="cell">613-555-2222</phone-number>
</contact-info>
</customer>
输出
{
"customer" : {
"personal-info" : {
"name" : "Jane Doe"
},
"contact-info" : {
"address" : {
"street" : "123 Any Street",
"city" : "My Town"
},
"phone-number" : [ {
"type" : "work",
"value" : "613-555-1111"
}, {
"type" : "cell",
"value" : "613-555-2222"
} ]
}
}
}
了解更多信息
关于spring - 在 Spring 中使用 MOXy JAXB 生成 JSON 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12162216/