在Spring中使用MOXy JAXB生成JSON输出

时间:2012-08-28 15:05:24

标签: spring jaxb eclipselink moxy

我需要编组json和xml输出。但是,如何在Spring中使用MOXy JAXB生成JSON输出?我可以生成xml文件,如示例中所示:http://wiki.eclipse.org/EclipseLink/Examples/MOXy/Spring/JAXBDynamicOXM

该文档没有输出json的任何示例。我知道我可以使用JAXB Marshaller通过设置jaxb属性来生成json输出。如何使用MOXy / Spring / JAXB做同样的事情?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

注意:我是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文档转换为动态模型,然后将其封送到JSON。请注意,上下文路径(本示例中为“forum12162216”)必须与包含jaxb.properties文件的包名称相对应。通过在"eclipselink.media-type"上设置Marshaller属性来启用JSON绑定。

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"
         } ]
      }
   }
}

了解更多信息