这是我的 Xml 输出
<worklist>
<work>
<relation-list target-type="artist">
<relation type="composer">
<name>Пётр Ильич Чайковский</name>
<sort-name>Пётр Ильич Чайковский</sort-name>
</relation>
<relation type="writer">
<name>Frank Turner/name>
<sort-name>Turner Frank</sort-name>
</relation>
</relation-list>
</work>
</worklist>
我正在尝试使用 EclipseLInk Moxy 来获取这个 json 输出
{
"work" : [ {
"relations" : [ {
"type" : "composer",
"name" : "Пётр Ильич Чайковский",
"sort-name" : "Пётр Ильич Чайковский"
}, {
"type" : "writer",
"name" : "frank turner",
"sort-name" : "turner, frank"
} ]
} ]
}
但我能做的最好的就是
{
"work" : [ {
"relationList" : [ {
"relations" : [ {
"type" : "composer",
"name" : "Пётр Ильич Чайковский",
"sort-name" : "Пётр Ильич Чайковский"
}, {
"type" : "writer",
"name" : "frank turner",
"sort-name" : "turner, frank"
} ]
} ],
} ]
}
注意我已经删除了关系列表的目标类型属性,并且我已经将关系重命名为关系,但是如果我尝试将关系列表与其父级合并使用
<java-type name="Work">
<java-attributes>
<xml-element java-attribute="relationList" xml-path="."/>
</java-attributes>
</java-type>
我得到以下异常
Caused by: java.lang.NullPointerException
at java.io.Writer.write(Writer.java:140)
at org.eclipse.persistence.oxm.record.JSONWriterRecord.writeKey(JSONWriterRecord.java:580)
at org.eclipse.persistence.oxm.record.JSONFormattedWriterRecord.openStartElement(JSONFormattedWriterRecord.java:147)
at org.eclipse.persistence.internal.oxm.XPathNode.startElement(XPathNode.java:359)
at org.eclipse.persistence.internal.oxm.XMLCompositeCollectionMappingNodeValue.marshalSingleValue(XMLCompositeCollectionMappingNodeValue.java:292)
at org.eclipse.persistence.internal.oxm.XMLCompositeCollectionMappingNodeValue.marshal(XMLCompositeCollectionMappingNodeValue.java:91)
at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:151)
at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:104)
at org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext.marshal(ObjectMarshalContext.java:60)
at org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:350)
at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:467)
at org.eclipse.persistence.internal.oxm.XMLCompositeCollectionMappingNodeValue.marshalSingleValue(XMLCompositeCollectionMappingNodeValue.java:299)
at org.eclipse.persistence.internal.oxm.XMLCompositeCollectionMappingNodeValue.marshal(XMLCompositeCollectionMappingNodeValue.java:91)
at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:151)
at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:104)
at org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext.marshal(ObjectMarshalContext.java:60)
at org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:350)
at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:467)
at org.eclipse.persistence.internal.oxm.XMLCompositeObjectMappingNodeValue.marshalSingleValue(XMLCompositeObjectMappingNodeValue.java:224)
at org.eclipse.persistence.internal.oxm.XMLCompositeObjectMappingNodeValue.marshal(XMLCompositeObjectMappingNodeValue.java:144)
at org.eclipse.persistence.internal.oxm.NodeValue.marshal(NodeValue.java:104)
at org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext.marshal(ObjectMarshalContext.java:60)
at org.eclipse.persistence.internal.oxm.XPathNode.marshal(XPathNode.java:350)
at org.eclipse.persistence.internal.oxm.TreeObjectBuilder.buildRow(TreeObjectBuilder.java:467)
at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:1074)
at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:689)
at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:602)
at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:584)
我想我理解这个问题。因为 Relation-List 元素同时包含关系元素和目标类型属性,所以直接转换为 json 需要关系列表元素来封装这两个元素,即我认为如果 Relation-List 元素没有目标类型属性在模型中它不会有问题。
但是我无论如何都不想输出目标类型,所以它应该可以工作,有没有办法解决这个问题?
编辑: 感谢您的回答,但它似乎与我正在做的事情相同,除了我尝试过的示例代码可以正常工作。我尝试在示例代码中添加更多元素以尝试破坏它但没有成功。
我最初的问题显示了一个比现实更简单的情况,所以在下面我实际上输出了完整的输出,以防有人可以看到可能导致问题的原因
{
"count" : 1,
"offset" : 0,
"work" : [ {
"id" : "4ff89cf0-86af-11de-90ed-001fc6f176ff",
"type" : "Opera",
"score" : "100",
"title" : "Symphony No. 5",
"language" : "eng",
"iswcs" : [ "T-101779304-1", "B-101779304-1" ],
"disambiguation" : "demo",
"aliases" : [ "Symp5" ],
"relation-list" : [ {
"relations" : [ {
"type" : "composer",
"direction" : "backward",
"attributes" : [ "additional" ],
"artist" : {
"id" : "1f9df192-a621-4f54-8850-2c5373b7eac9",
"name" : "Пётр Ильич Чайковский",
"sort-name" : "Пётр Ильич Чайковский"
}
}, {
"type" : "writer",
"direction" : "backward",
"artist" : {
"id" : "abcdefgh-a621-4f54-8850-2c5373b7eac9",
"name" : "frank",
"sort-name" : "turner"
}
} ]
} ],
"tags" : [ {
"count" : 10,
"name" : "classical"
} ]
} ]
}
工作正常,但添加
<java-type name="Work">
<java-attributes>
<xml-element java-attribute="relationList" xml-path="."/>
</java-attributes>
</java-type>
导致堆栈跟踪。
也许堆栈跟踪包含解决方案
更新
完整的项目可在线获取
数据模型在http://svn.musicbrainz.org/mmd-schema/trunk/brainz-mmd2-jaxb/ 下载并运行 mvn install
然后使用这个的项目在http://svn.musicbrainz.org/search_server/trunk/ 下载并运行 mvn 包,
这包含三个子项目,问题之一是 servlet 项目,以复制问题:
光盘小服务程序
编辑 src/main/resources/oxml.xml 更改
<xml-element java-attribute="relationList" name="relationList"/>
至
<xml-element java-attribute="relationList" xml-path="."/>
mvn包
src/test/java/org/musicbrainz/search/servlet/FindWorkTest 现在将在 testOutputAsJsonNew() 和 testOutputAsJsonNewPretty() 上失败
进一步更新 Blaise 解决了示例没有显示的问题(我不知道)你实际上可以有多个关系列表元素,即 Xml 可能是
<worklist>
<work>
<relation-list target-type="artist">
<relation type="composer">
<name>Jane Doe</name>
<sort-name>Doe Jane</sort-name>
</relation>
<relation type="writer">
<name>Frank Turner</name>
<sort-name>Turner Frank</sort-name>
</relation>
</relation-list>
<relation-list target-type="release">
<relation type="cover">
<name>Hey Jude</name>
<sort-name>Hey Jude</sort-name>
</relation>
</relation-list>
</work>
</worklist>
我想要输出的是
{
"work" : [ {
"relations" : [ {
"type" : "composer",
"name" : "Jane Doe",
"sort-name" : "Doe Jane"
}, {
"type" : "writer",
"name" : "Frank Turner",
"sort-name" : "Turner Frank"
}, {
"type" : "cover",
"name" : "Hey Jude",
"sort-name" : "Hey Jude"
} ]
} ]
}
而我能得到的最好的
{
"work" : [ {
"relationLists" : [ {
"relations" : [ {
"type" : "composer",
"name" : "Jane Doe",
"sort-name" : "Doe Jane"
}, {
"type" : "writer",
"name" : "Frank Turner",
"sort-name" : "Turner Frank"
} ]
}, {
"relations" : [ {
"type" : "cover",
"name" : "Hey Jude",
"sort-name" : "Hey Jude"
} ]
} ]
} ]
}
所以本质上我想合并所有的关系,这样它们就在一个列表中并丢失了关系列表标签。我们合并了不同的关系列表并丢弃了标识每个关系列表的目标类型属性,但我们不需要能够识别这些子列表,这可能看起来很奇怪。
是否有可能做到这一点 ?