【问题标题】:Generate JSON output using MOXy JAXB in Spring在 Spring 中使用 MOXy JAXB 生成 JSON 输出
【发布时间】:2012-08-28 15:05:23
【问题描述】:

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

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

感谢您的帮助!

【问题讨论】:

    标签: spring jaxb eclipselink moxy


    【解决方案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 文件的包名称。 JSON 绑定通过在Marshaller 上设置"eclipselink.media-type" 属性来启用。

    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/Moxy 示例中所述,我正在使用 Jaxb2Marshaller,而 Jaxb2Marshaller 似乎没有 setProperty 方法。如何使用 jaxb2marshaller 打印到 Sysout 或浏览器?另外,我使用的是Maven,绑定oxm.xml文件应该放在哪里?,我有jaxb.properties与模型类在同一个包中。
    • @Ngun - 有一个 initJaxbMarshaller(Marshaller) 方法看起来很有前途,但您需要实现 Jaxb2Marshaller 的子类才能利用它:static.springsource.org/spring-ws/sites/1.5/apidocs/org/…。这是一个有助于 Maven 方面的示例的链接:github.com/bdoughan/blog20120418
    • @Ngun - 在指定 OXM 元数据文件的属性映射中,您还可以将 "eclipselink.media-type" 属性设置为 "application/json"
    猜你喜欢
    • 1970-01-01
    • 2011-08-26
    • 2013-09-09
    • 2015-04-03
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 2014-05-15
    相关资源
    最近更新 更多