简体   繁体   English

MOXy DynamicEntity与JSON吗?

[英]MOXy DynamicEntity with JSON?

I'm trying to use DynamicEntity to unmarshal some simple JSON, and it's totally bombing on me. 我正在尝试使用DynamicEntity解组一些简单的JSON,这完全炸了我。 Docs are rather sparse, is it even possible to do this? 文档相当稀疏,甚至有可能这样做吗? I'm basically doing this; 我基本上是这样做的;

JAXBContext jaxbContext = JAXBContext.newInstance(DynamicEntity.class);
DynamicEntity entity = (DynamicEntity) jaxbContext.createUnmarshaller().unmarshal(entityStream);

This is straight from the XML docs here: https://wiki.eclipse.org/EclipseLink/Examples/MOXy/Dynamic/XmlToDynamicEntity 这直接来自XML文档: https//wiki.eclipse.org/EclipseLink/Examples/MOXy/Dynamic/XmlToDynamicEntity

And I get; 我明白了 Caused by: 造成原因:

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
org.eclipse.persistence.dynamic.DynamicEntity is an interface, and JAXB can't handle interfaces.
    this problem is related to the following location:
        at org.eclipse.persistence.dynamic.DynamicEntity

Has anyone managed to get this to work? 有没有人设法使它起作用? I'm basically trying to avoid building POJOs since my backend store doesnt care about them anyway, I just want to deserialize to a generic object and pass it along. 我基本上试图避免构建POJO,因为我的后端存储无论如何都不关心它们,我只想反序列化为通用对象并将其传递。 In .NET I'd just use dynamic but I'm pretty stumped on how to do this with Moxy. 在.NET中,我只使用dynamic但是我对如何使用Moxy做到这一点感到很困惑。

In order to get DynamicEntity , it is neccessary to use DynamicJAXBContext . 为了获得DynamicEntity ,有必要使用DynamicJAXBContext It can be configured using the following in jaxb.properties file: 可以使用jaxb.properties文件中的以下内容进行配置:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory

or you can use DynamicJAXBContextFactory directly. 或者您可以直接使用DynamicJAXBContextFactory

Although Dynamic MOXY does not require any java classes for the bindings (the POJOs), you need to provide binding metadata when creating the context. 尽管Dynamic MOXY不需要任何Java类用于绑定(POJO),但是在创建上下文时需要提供绑定元数据。 It can be XSD or binding.xml that describes the mapping. 它可以是描述映射的XSD或binding.xml。 So instead of creating the context using JAXBContext.newInstance(DynamicEntity.class) , you would need to use alternative methods - see links below. 因此,您无需使用JAXBContext.newInstance(DynamicEntity.class)创建上下文,而需要使用其他方法-请参见下面的链接。

See example here (using XSD): https://wiki.eclipse.org/EclipseLink/Examples/MOXy/Dynamic/JAXBContextFromXMLSchema 请参阅此处的示例(使用XSD): https : //wiki.eclipse.org/EclipseLink/Examples/MOXy/Dynamic/JAXBContextFromXMLSchema

or more complex example using binding.xml and also JSON: https://wiki.eclipse.org/EclipseLink/Examples/MOXy/Dynamic/Flickr 或更复杂的示例(使用binding.xml和JSON): https : //wiki.eclipse.org/EclipseLink/Examples/MOXy/Dynamic/Flickr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM