【问题标题】:Can I get MOXy to not output an attribute when generating json?我可以让 MOXy 在生成 json 时不输出属性吗?
【发布时间】:2012-07-16 04:50:25
【问题描述】:

我的 JAXB 对象模型的一个实例包含一个属性,当我为该实例生成 Xml 而不是在生成 json 时输出该属性

我想要

<release-group type="Album">
<title>Fred</title>
</release-group>

"release-group" : {
         "title" : "fred",
       },

但有

"release-group" : {
         "type" : "Album",
         "title" : "fred"
      },         

我可以使用 oxml.xml 映射文件来做到这一点吗

【问题讨论】:

    标签: java json jaxb eclipselink moxy


    【解决方案1】:

    由于您的 JSON 绑定与您的 XML 绑定略有不同,我将使用 EclipseLink JAXB (MOXy) 的外部映射文件。

    oxm.xml

    在外部映射文件中,我们将type 字段标记为瞬态。

    <?xml version="1.0"?>
    <xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="forum383861">
        <java-types>
            <java-type name="ReleaseGroup">
                <java-attributes>
                    <xml-transient java-attribute="type"/>
                </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    发布组

    下面是我将用于此示例的域模型。注意type 属性是如何用@XmlAttribute 注释的。

    package forum383861;
    
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement(name="release-group")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class ReleaseGroup {
    
        @XmlAttribute
        String type;
    
        String title;
    
    }
    

    jaxb.properties

    要将 MOXy 指定为您的 JAXB 提供程序,您需要在与域模型相同的包中包含一个名为 jaxb.properties 的文件,并使用以下条目(请参阅:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html)。

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
    

    演示

    由于 XML 和 JSON 表示不同,我们将为它们创建单独的 JAXBContexts。对于 JSON,我们将利用 MOXy 的外部映射文件。

    package forum383861;
    
    import java.util.*;
    import javax.xml.bind.*;
    import org.eclipse.persistence.jaxb.JAXBContextProperties;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            ReleaseGroup rg = new ReleaseGroup();
            rg.type = "Album";
            rg.title = "Fred";
    
            // XML
            JAXBContext xmlJC = JAXBContext.newInstance(ReleaseGroup.class);
            Marshaller xmlMarshaller = xmlJC.createMarshaller();
            xmlMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            xmlMarshaller.marshal(rg, System.out);
    
            // JSON
            Map<String, Object> properties = new HashMap<String, Object>(2);
            properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum383861/oxm.xml");
            properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
            JAXBContext jsonJC = JAXBContext.newInstance(new Class[] {ReleaseGroup.class}, properties);
            Marshaller jsonMarshaller = jsonJC.createMarshaller();
            jsonMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jsonMarshaller.marshal(rg, System.out);
        }
    
    }
    

    输出

    下面是运行演示代码的输出:

    <?xml version="1.0" encoding="UTF-8"?>
    <release-group type="Album">
       <title>Fred</title>
    </release-group>
    {
       "release-group" : {
          "title" : "Fred"
       }
    }
    

    【讨论】:

    • 顺便说一句 @Blaise Doughan 你的回复太棒了,但我查看了 eclipselink 网站,找不到任何记录在那里的信息,是吗?
    猜你喜欢
    • 1970-01-01
    • 2012-07-12
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    相关资源
    最近更新 更多