简体   繁体   English

JAX-B / JSON:继承,复合模式,非编组异常

[英]JAX-B / JSON: inheritance, composite-pattern, unmarshal exception

i'm working on a RESTful WebService (NetBeans + Jersey) and trying to realize a UI-Description (Label, Buttons,...) for mobile Apps (Android). 我正在研究RESTful WebService(NetBeans + Jersey),并试图实现针对移动应用程序(Android)的UI描述(标签,按钮等)。

I used the Composite-Design-Pattern to create a UIContainer, UIBasicElements, UIComplexElements, and so on... 我使用Composite-Design-Pattern来创建UIContainer,UIBasicElements,UIComplexElements等。

My Problem is: The UIContainer contains a private List<UIComponent> uiComponents; 我的问题是:UIContainer包含一个private List<UIComponent> uiComponents; .

If i add 如果我添加

@XmlSeeAlso( { 
    UIBasicLabel.class, 
    UIBasicIcon.class, 
    UIBasicButton.class,
    ... 
} )

to the class public abstract class UIComponent i get the JSON-Format i want, ie something like 到类public abstract class UIComponent我得到了我想要的JSON格式,即类似

{ ..., uiComponents : [ {Label} , {Icon} , ... ] , ... }

which i can process on my Android-Client. 我可以在我的Android客户端上进行处理。 I also noticed, that a @type : ClassName property is added to the JSON-Output. 我还注意到, @type : ClassName属性被添加到JSON-Output中。 Isn't this added to give the unmarshaller a hint, what class to use? 是否不是为了给解组员一个提示而添加的,使用什么类?

With the Jersey-Client i get an Exception on unmarshalling: 使用Jersey-Client,我在解组时遇到异常:

Exception in thread "main" javax.ws.rs.WebApplicationException: javax.xml.bind.UnmarshalException
 - with linked exception:
[com.sun.istack.internal.SAXParseException2; lineNumber: 0; columnNumber: 0; Unable to create an instance of de.materna.mobile.management.entities.uidescription.UIComponent]

As far as i understand, i need to modify the UIContainer.class to 据我了解,我需要将UIContainer.class修改为

@XmlElementRef
private List<UIComponent> uiComponents;

so the unmarshaller knows what to deserialize here. 因此,解组员知道要在这里反序列化什么。

However, my Problem is, with @XmlElementRef the JSON becomes this: 但是,我的问题是,使用@XmlElementRef ,JSON变为:

{ ..., Label : [ ... ] , Icon : [ ... ], ... }

instead of 代替

{ ..., uiComponents : [ {Label} , {Icon} , ... ] , ... }

Is there any way to accomplish both? 有什么办法可以做到这两者?

Thank you very much! 非常感谢你!

PS: sorry for my bad english - i hope it's not too hard to read/understand PS:对不起,我的英语不好-我希望阅读/理解不是太难

从描述对象模型的XML模式开始,然后运行xjc或wsimport(来自JDK的工具),它们将生成您的Java类。

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

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