生成json时,我可以让MOXy不输出属性吗? [英] Can I get MOXy to not output an attribute when generating json?

查看:88
本文介绍了生成json时,我可以让MOXy不输出属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

An instance of my JAXB Object model contains an attribute that I want output when I generate Xml for the instance but not when I generate json

即我希望

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

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

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

可以我使用oxml.xml映射文件执行此操作

Can I do this using the oxml.xml mapping file

推荐答案

由于您的JSON绑定与XML绑定略有不同,我将使用< a href =http://www.eclipse.org/eclipselink/moxy.php =nofollow> EclipseLink JAXB(MOXy) 的外部映射文件。

Since your JSON binding is slightly different from your XML binding I would use EclipseLink JAXB (MOXy)'s external mapping file.

oxm.xml

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

In the external mapping file we will mark the type field as transient.

<?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>

ReleaseGroup

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

Below is the domain model I'll use for this example. Note how the type property is annotated with @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 / specify-eclipselink-moxy-as-your.html

To specify MOXy as your JAXB provider you need to include a file called jaxb.properties in the same package as your domain model with the following entry (see: 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的外部映射文件。

Since the XML and JSON representations are different we'll create separate JAXBContexts for them. For the JSON one we'll leverage MOXy's external mapping file.

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

}

输出

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

Below is the output from running the demo code:

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

这篇关于生成json时,我可以让MOXy不输出属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆