【问题标题】:Can I use Eclipselink MOXy to move an element to become a key when outputting json输出json时可以使用Eclipselink MOXy移动元素成为键吗
【发布时间】:2012-07-18 09:10:02
【问题描述】:

从我的 JAXB 模型我可以输出这个 xml

<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#" xmlns:ext="http://musicbrainz.org/ns/ext#-2.0">
    <work-list>
        <work id="4ff89cf0-86af-11de-90ed-001fc6f176ff">
            <relation-list target-type="artist">
                <relation type="composer">
                    <direction>backward</direction>
                </relation>
            </relation-list>
        </work>
    </work-list>
</metadata>

目前使用 MOXy 和 oxml.xml 我可以输出以下 JSON

{
   "work" : [ {
      "relations": [ {
         "target-type" : "artist",
         "relation" : [ {
            "type" : "composer",
            "direction" : "backward",
            },
         } ]
      } ]
   } ]
}

(在我的 oxml.xml 中,我扁平化了工作列表和关系列表对象,并将关系重命名为关系。)

但实际要求是更复杂的转换,我不知道如何用正确的术语解释它,但这里是所需输出的示例。

{
   "work" : [ {
      "relations": {
         "artist": [{
            "direction": "backward",
            "type": "composer",            
          }
         ],
       }
   }]
}

这可以在 eclipselink MOXy 中完成吗?

【问题讨论】:

  • 是否有一组固定的目标类型,或者可以是什么?
  • 一个固定的列表,5个不同的值。

标签: java json jaxb eclipselink moxy


【解决方案1】:

以下是如何做到这一点的示例:

注册表

您将需要一个带有 @XmlRegistry 注释的类,该类对于五个不同的元素值中的每一个都有用 @XmlElementDecl 标记的方法。

package forum11537931;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;

@XmlRegistry
public class Registry {
    private static final String ARTIST = "artist";
    private static final String FOO = "foo";

    @XmlElementDecl(name=ARTIST)
    public JAXBElement<Relation> createArtist(Relation relation) {
        return new JAXBElement<Relation>(new QName(ARTIST), Relation.class, relation);
    }

    @XmlElementDecl(name=FOO, substitutionHeadName=ARTIST)
    public JAXBElement<Relation> createFoo(Relation relation) {
        return new JAXBElement<Relation>(new QName(FOO), Relation.class, relation);
    }

}

RelationsAdapter

我们将使用XmlAdapterRelations 对象转换为更好地映射到JSON 表示的对象。我们将利用@XmlElementRef 注释来执行此操作(请参阅http://blog.bdoughan.com/2010/12/represent-string-values-as-element.html)。

package forum11537931;

import java.util.*;

import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.namespace.QName;

public class RelationsAdapter extends XmlAdapter<RelationsAdapter.AdaptedRelations, Relations> {

    @Override
    public Relations unmarshal(AdaptedRelations v) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public AdaptedRelations marshal(Relations relations) throws Exception {
        AdaptedRelations adaptedRelations = new AdaptedRelations();
        for(Relation relation : relations.relations) {
            adaptedRelations.relations.add(new JAXBElement<Relation>(new QName(relations.targetType), Relation.class, relation));
        }
        return adaptedRelations;
    }

    @XmlSeeAlso({Registry.class})
    public static class AdaptedRelations {

        @XmlElementRef(type=JAXBElement.class, name="artist")
        public List<JAXBElement<Relation>> relations = new ArrayList<JAXBElement<Relation>>();

    }

}

oxm.xml

由于您不希望 XmlAdapter 应用于 XML 表示,我们将使用 MOXy 的外部映射文档指定它(请参阅 http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html)。

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum11537931">
    <java-types>
        <java-type name="Work">
            <java-attributes>
                <xml-element java-attribute="relations">
                    <xml-java-type-adapter value="forum11537931.RelationsAdapter"/>
                </xml-element>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

input.xml

出于本示例的目的,我简化了您的 XML 文档。

<?xml version="1.0" encoding="UTF-8"?>
<work id="4ff89cf0-86af-11de-90ed-001fc6f176ff">
    <relation-list target-type="artist">
        <relation type="composer">
            <direction>backward</direction>
        </relation>
    </relation-list>
</work>

演示

package forum11537931;

import java.io.File;
import java.util.*;
import javax.xml.bind.*;

import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        // XML
        JAXBContext jc = JAXBContext.newInstance(Work.class, Registry.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum11537931/input.xml");
        Work work = (Work) unmarshaller.unmarshal(xml);

        // JSON
        Map<String, Object> properties = new HashMap<String, Object>(2);
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum11537931/oxm.xml");
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        JAXBContext jsonJC = JAXBContext.newInstance(new Class[] {Work.class, Registry.class}, properties);
        Marshaller jsonMarshaller = jsonJC.createMarshaller();
        jsonMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jsonMarshaller.marshal(work, System.out);
    }

}

输出

{
   "work" : {
      "relations" : [ {
         "artist" : [ {
            "type" : "composer",
            "direction" : "backward"
         } ]
      } ]
   }
}

领域模型

工作

package forum11537931;

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Work {

    @XmlElement(name="relation-list")
    List<Relations> relations;

}

关系

package forum11537931;

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Relations {

    @XmlAttribute(name="target-type")
    String targetType;

    @XmlElement(name="relation")
    List<Relation> relations = new ArrayList<Relation>();

}

关系

package forum11537931;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Relation {

    @XmlAttribute
    String type;

    String direction;

}

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

【讨论】:

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