【问题标题】:Can I get MOXy to not output an element when generating json?我可以让 MOXy 在生成 json 时不输出元素吗?
【发布时间】:2012-09-08 22:13:23
【问题描述】:

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

我想要

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

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

但是有

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

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

这个答案显示了我如何使用瞬态关键字Can I get MOXy to not output an attribute when generating json? 对属性执行此操作,但我无法将其用于元素。

【问题讨论】:

    标签: json eclipselink moxy


    【解决方案1】:

    抱歉问题解决了,我有点困惑。

    我上面给出的例子实际上并没有准确匹配真实情况,type实际上是作为Xml的一个属性输出的,但是使用transient不起作用,因为它已经在JAXB中重命名了

    @XmlAttribute(name = "target-type", required = true)
    @XmlSchemaType(name = "anyURI")
    protected String targetType;
    

    所以添加

     <java-type name="ReleaseGroup">
                <java-attributes>
                    <xml-transient java-attribute="targetType"/>
                </java-attributes>
            </java-type>
    

    工作,以前我做错了

     <java-type name="ReleaseGroup">
                <java-attributes>
                    <xml-transient java-attribute="target-type"/>
                </java-attributes>
            </java-type>
    

    【讨论】:

      猜你喜欢
      • 2012-07-16
      • 2012-07-12
      • 1970-01-01
      • 2012-07-12
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多