栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

MOXy反序列化异常:在项目中找不到带有默认根元素的描述符

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MOXy反序列化异常:在项目中找不到带有默认根元素的描述符

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

问题#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的问题:是否有一种方法可以使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 /输出

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/406876.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号