java MOXy 反序列化异常:在项目中找不到具有默认根元素的描述符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14246033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MOXy deserialization exception: A descriptor with default root element was not found in the project
提问by Behzad Pirvali
Here are my classes:
这是我的课程:
@XmlRootElement(name="Zoo")
class Zoo {
//@XmlElementRef
public Collection<? extends Animal> animals;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({Bird.class, Cat.class, Dog.class})
@XmlDiscriminatorNode("@type")
abstract class Animal {
@XmlElement
public String name;
}
@XmlDiscriminatorValue("Bird")
@XmlRootElement(name="Bird")
class Bird extends Animal {
@XmlElement
public String wingSpan;
@XmlElement
public String preferredFood;
}
@XmlDiscriminatorValue("Cat")
@XmlRootElement(name="Cat")
class Cat extends Animal {
@XmlElement
public String favoriteToy;
}
@XmlDiscriminatorValue("Dog")
@XmlRootElement(name="Dog")
class Dog extends Animal {
@XmlElement
public String breed;
@XmlElement
public String leashColor;
}
Here is the serialized JSON:
这是序列化的 JSON:
{
"animals": [
{
"type": "Bird",
"name": "bird-1",
"wingSpan": "6 feets",
"preferredFood": "food-1"
},
{
"type": "Cat",
"name": "cat-1",
"favoriteToy": "toy-1"
},
{
"type": "Dog",
"name": "dog-1",
"breed": "bread-1",
"leashColor": "black"
}
]
}
Here is the de-serializer code:
这是反序列化器代码:
public static <T> T Deserialize_Moxy(String jsonStr, Class<?>[] cl) throws JAXBException {
InputStream is = new ByteArrayInputStream(jsonStr.getBytes());
JAXBContext jc = JAXBContext.newInstance(cl);
Unmarshaller unmarshaller = jc.createUnmarshaller();
// Marshal to JSON
unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
unmarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
@SuppressWarnings("unchecked")
T obj = (T)unmarshaller.unmarshal(is);
return obj;
}
Here is the exception:
这是例外:
Exception in thread "main" javax.xml.bind.UnmarshalException
- with linked exception:
[Exception [EclipseLink-25008] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: A descriptor with default root element was not found in the project]
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.handleXMLMarshalException(JAXBUnmarshaller.java:1014)
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:147)
at com.bp.samples.json.generics.Foo.Deserialize_Moxy(Foo.java:271)
at com.bp.samples.json.generics.Foo.main(Foo.java:111)
Caused by: Exception [EclipseLink-25008] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: A descriptor with default root element was not found in the project
at org.eclipse.persistence.exceptions.XMLMarshalException.noDescriptorWithMatchingRootElement(XMLMarshalException.java:143)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshallerHandler.startElement(SAXUnmarshallerHandler.java:222)
at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parseRoot(JSONReader.java:161)
at org.eclipse.persistence.internal.oxm.record.json.JSONReader.parse(JSONReader.java:118)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:827)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:350)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:334)
at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:407)
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:133)
... 2 more
Also a question on the serialized JSON: Is there a way to get the JSON serializer to publish "@type" instead of "type". Currently, it looks like the objects having the property "type". If we could decorate it with "@", it will be more obvious that this is more of a type info than a property.
还有一个关于序列化 JSON 的问题:有没有办法让 JSON 序列化程序发布“@type”而不是“type”。目前,它看起来像具有“类型”属性的对象。如果我们可以用“@”来装饰它,那么它更像是一个类型信息而不是一个属性会更明显。
Thanks, Behzad
谢谢,贝扎德
回答by bdoughan
Below are my answers to your two questions:
下面是我对你的两个问题的回答:
Question #1 - Exception
问题 1 - 异常
When you use the MarshallerProperties.JSON_INCLUDE_ROOT
property to turn off root elements then you need to use one of the unmarshal
methods that takes a Class
parameter to tell MOXy the type of object you wish to unmarshal.
当您使用该MarshallerProperties.JSON_INCLUDE_ROOT
属性关闭根元素时,您需要使用其中一种unmarshal
方法接受一个Class
参数来告诉 MOXy 您希望解组的对象类型。
StreamSource json = new StreamSource("src/forum14246033/input.json");
Zoo zoo = unmarshaller.unmarshal(json, Zoo.class).getValue();
Question #2
问题2
Also a question on the serialized JSON: Is there a way to get the JSON serializer to publish "@type" instead of "type". Currently, it looks like the objects having the property "type". If we could decorate it with "@", it will be more obvious that this is more of a type info than a property.
还有一个关于序列化 JSON 的问题:有没有办法让 JSON 序列化程序发布“@type”而不是“type”。目前,它看起来像具有“类型”属性的对象。如果我们可以用“@”来装饰它,那么它更像是一个类型信息而不是一个属性会更明显。
The @
prefix indicates that a field/property maps to an XML attribute. You can use the JAXBContextProperties.JSON_ATTRIBUTE_PREFIX
property to specify a prefix to qualify data that was mapped to an XML attribute.
该@
前缀表示字段/属性映射为XML属性。您可以使用该JAXBContextProperties.JSON_ATTRIBUTE_PREFIX
属性指定前缀来限定映射到 XML 属性的数据。
properties.put(JAXBContextProperties.JSON_ATTRIBUTE_PREFIX, "@");
FULL EXAMPLE
完整示例
Demo
演示
package forum14246033;
import java.util.*;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(2);
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
properties.put(JAXBContextProperties.JSON_ATTRIBUTE_PREFIX, "@");
JAXBContext jc = JAXBContext.newInstance(new Class[] {Zoo.class}, properties);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource json = new StreamSource("src/forum14246033/input.json");
Zoo zoo = unmarshaller.unmarshal(json, Zoo.class).getValue();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(zoo, System.out);
}
}
input.json/Output
输入.json/输出
{
"animals" : [ {
"@type" : "Bird",
"name" : "bird-1",
"wingSpan" : "6 feets",
"preferredFood" : "food-1"
}, {
"@type" : "Cat",
"name" : "cat-1",
"favoriteToy" : "toy-1"
}, {
"@type" : "Dog",
"name" : "dog-1",
"breed" : "bread-1",
"leashColor" : "black"
} ]
}
DOMAIN MODEL
域模型
I don't recommend using public fields in your domain model, but if you go that way you can reduce your metadata down to the following:
我不建议在您的域模型中使用公共字段,但如果您这样做,您可以将元数据减少到以下内容:
Zoo
动物园
import java.util.Collection;
class Zoo {
public Collection<? extends Animal> animals;
}
Animal
动物
import javax.xml.bind.annotation.XmlSeeAlso;
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorNode;
@XmlSeeAlso({Bird.class, Cat.class, Dog.class})
@XmlDiscriminatorNode("@type")
abstract class Animal {
public String name;
}
Bird
鸟
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorValue;
@XmlDiscriminatorValue("Bird")
class Bird extends Animal {
public String wingSpan;
public String preferredFood;
}
jaxb.properties
jaxb.properties
To specify MOXy as your JAXB (JSR-222) provider you need to include a file called jaxb.properties
in the same package as your domain model with the following entry:
要将 MOXy 指定为您的 JAXB (JSR-222) 提供程序,您需要包含一个jaxb.properties
在与域模型相同的包中调用的文件,其中包含以下条目:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory