2

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

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

感谢你的帮助!

4

1 回答 1

2

注意: 我是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 绑定通过"eclipselink.media-type"Marshaller.

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);
    }

}

客户.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"
         } ]
      }
   }
}

了解更多信息

于 2012-08-28T15:41:28.647 回答