【问题标题】:MOXy deserialization exception: A descriptor with default root element was not found in the projectMOXy 反序列化异常:在项目中未找到具有默认根元素的描述符
【发布时间】:2013-01-09 20:39:29
【问题描述】:

这是我的课程:

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

这是序列化的 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"
            }
        ]
    }

这是反序列化代码:

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

这是一个例外:

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

还有一个关于序列化 JSON 的问题:有没有办法让 JSON 序列化程序发布“@type”而不是“type”。目前,它看起来像具有“类型”属性的对象。如果我们可以用“@”来装饰它,那么它更像是一个类型信息而不是一个属性会更明显。

谢谢, 贝扎德

【问题讨论】:

    标签: java json moxy


    【解决方案1】:

    以下是我对您的两个问题的回答:

    问题 #1 - 例外

    当您使用MarshallerProperties.JSON_INCLUDE_ROOT 属性关闭根元素时,您需要使用unmarshal 方法之一,该方法采用Class 参数来告诉MOXy 您希望解组的对象类型。

    StreamSource json = new StreamSource("src/forum14246033/input.json");
    Zoo zoo = unmarshaller.unmarshal(json, Zoo.class).getValue();
    

    问题 #2

    还有一个关于序列化 JSON 的问题:Is there a way to get the JSON 序列化程序发布“@type”而不是“type”。目前看来 像具有“类型”属性的对象。如果我们可以装饰它 使用“@”,更明显的是这更像是一种类型信息 而不是一个属性。

    @ 前缀表示字段/属性映射到 XML 属性。您可以使用 JAXBContextProperties.JSON_ATTRIBUTE_PREFIX 属性指定前缀来限定映射到 XML 属性的数据。

    properties.put(JAXBContextProperties.JSON_ATTRIBUTE_PREFIX, "@");
    

    完整示例

    演示

    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

    {
       "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"
       } ]
    }
    

    域模型

    我不建议在您的域模型中使用公共字段,但如果您这样做,您可以将元数据减少到以下内容:

    动物园

    import java.util.Collection;
    
    class Zoo {
        public Collection<? extends Animal> animals;
    }
    

    动物

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

    鸟

    import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorValue;
    
    @XmlDiscriminatorValue("Bird")
    class Bird extends Animal {
        public String wingSpan;
        public String preferredFood;
    }
    

    jaxb.properties

    要将 MOXy 指定为您的 JAXB (JSR-222) 提供程序,您需要在与您的域模型相同的包中包含一个名为 jaxb.properties 的文件,其中包含以下条目:

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
    

    【讨论】:

    • 非常感谢。该修改使反序列化器工作。关于问题 #2,我知道,所以也许 '@' 不是最好的指标,因为正如你所说,它显示了 xml 属性。建议的原因是,在 JACKSON 方面,我看到元数据是使用“@”注入的(如 @class com....)。话虽如此,您是否同意以某种方式装饰“type”(如“#type”)以表明这不是对象属性会更好。非常感谢你
    • 实际上推荐'@'符号的原因是基于我的理解JSON解析器知道它。所以,理想情况下,'type' 应该用这样的东西装饰:“@xsi:type”。这也清楚地表明 type 不是 xml 属性。
    • @BehzadPirvali - 我肯定见过有人像继承指标这样对节点进行限定。这个答案实际上是 @ 前缀的因素。我已更新我的答案以包含 JSON 文档。
    • 对不起,我不确定我是否理解你的最后一句话。 “包含 JSON 文档”是什么意思?
    • @BehzadPirvali - 我的意思是我更新了我的答案以包含由我最初发布的演示代码生成/使用的 JSON 文档。
    猜你喜欢
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多