这是我的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"
} ]
} ],
} ]
}
注意我已经删除了relationList的target-type属性,并且我已经重命名了关系的关系但是如果我尝试使用
将relationList与其父类合并<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元素来封装这两个元素,即我认为如果Relation-List元素没有target-type属性在模型中它没有问题。
但是我不想输出目标类型,所以它应该有效,有没有办法解决这个问题?
修改 感谢您的回答,但它似乎与我正在做的事情相同,只是我尝试过的示例代码工作正常。我尝试在示例代码中添加更多元素,试图破解它但没有成功。
我原来的问题显示了一个更简单的案例然后是现实,所以在Ive下面实际输出完整的输出,万一有人可以看到可能导致问题的原因
{
"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项目,以复制问题:
cd servlet
编辑src / main / resources / oxml.xml更改
<xml-element java-attribute="relationList" name="relationList"/>
到
<xml-element java-attribute="relationList" xml-path="."/>
mvn package
src / test / java / org / musicbrainz / search / servlet / FindWorkTest现在将失败 testOutputAsJsonNew()和testOutputAsJsonNewPretty()
进一步更新 Blaise解决了这个例子没有显示的问题(我不知道)你实际上可以有多个relationList元素,即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"
} ]
} ]
} ]
}
所以基本上我想合并所有关系,因此它们在一个列表中并丢失关系列表标签。我们合并不同的关系列表并抛弃标识每个关系列表的目标类型属性似乎很奇怪,但我们不需要能够识别这些子列表。
是否可以这样做?
答案 0 :(得分:2)
更新#2
根据调查结果,我更新了示例代码,使用XmlAdapter
代替.
XPath。这种方法对你的用例会更好。
更新#1
从堆栈跟踪中看起来好像你可能在不允许的集合属性上设置self(“。”)XPath(我们将更改代码以便抛出更好的异常)。
在Work
类中,您有一个名为relationList
@XmlElement(name = "relation-list")
protected List<RelationList> relationList;
问题出现了,因为您在元数据文件中尝试将xml-path
设置为'.'
。
<xml-element java-attribute="relationList" xml-path="."/>
相反,您需要在引用包含该集合的对象的属性上使用'.
'路径。见下面的例子:
XML ADAPTER
.
可以更好地处理您的用例,而不是使用XmlAdapter
XPath。 XmlAdapter
允许我们将一个对象结构转换为另一个更好地映射到所需输入/输出的对象结构。对于此用例,XmlAdapter
看起来像。
package forum12338288;
import java.util.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class WorkAdapter extends XmlAdapter<WorkAdapter.AdaptedWork, Work> {
public static class AdaptedWork {
public List<Relation> relations = new ArrayList<Relation>();
}
@Override
public AdaptedWork marshal(Work work) throws Exception {
AdaptedWork adaptedWork = new AdaptedWork();
for(RelationList relationList : work.relationList) {
for(Relation relation : relationList.relation) {
adaptedWork.relations.add(relation);
}
}
return adaptedWork;
}
@Override
public Work unmarshal(AdaptedWork adaptedWork) throws Exception {
Work work = new Work();
RelationList relationList = new RelationList();
for(Relation relation : adaptedWork.relations) {
relationList.relation.add(relation);
}
work.relationList.add(relationList);
return work;
}
}
映射文档
您的地图文档应如下所示:
<强> oxm.xml 强>
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum12338288">
<java-types>
<java-type name="WorkList">
<java-attributes>
<xml-element java-attribute="work">
<xml-java-type-adapter value="forum12338288.WorkAdapter"/>
</xml-element>
</java-attributes>
</java-type>
<java-type name="RelationList">
<java-attributes>
<xml-transient java-attribute="targetType"/>
<xml-element java-attribute="relation" name="relations"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
DEMO CODE
下面的演示代码演示了如何利用映射文档读取XML并输出所需的JSON。
的演示强> 的
package forum12338288;
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(WorkList.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum12338288/input.xml");
WorkList workList = (WorkList) unmarshaller.unmarshal(xml);
// JSON
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum12338288/oxm.xml");
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
JAXBContext jsonJC = JAXBContext.newInstance(new Class[] {WorkList.class}, properties);
Marshaller marshaller = jsonJC.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(workList, System.out);
}
}
的 input.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"
} ]
} ]
}
JAVA模型
以下是我用来确保一切正常的Java模型。
的工作列表强> 的
package forum12338288;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement(name="worklist")
@XmlAccessorType(XmlAccessType.FIELD)
public class WorkList {
List<Work> work;
}
<强>工作强> 的
package forum12338288;
import java.util.*;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Work {
@XmlElement(name="relation-list")
List<RelationList> relationList = new ArrayList<RelationList>();
}
的 RelationList 强> 的
package forum12338288;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class RelationList {
@XmlAttribute(name="target-type")
String targetType;
List<Relation> relation;
}
的关系强> 的
package forum12338288;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Relation {
@XmlAttribute
String type;
String name;
@XmlElement(name="sort-name")
String sortName;
}
的 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