繁体   English   中英

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

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

我正在研究RESTful WebService(NetBeans + Jersey),并试图实现针对移动应用程序(Android)的UI描述(标签,按钮等)。

我使用Composite-Design-Pattern来创建UIContainer,UIBasicElements,UIComplexElements等。

我的问题是:UIContainer包含一个private List<UIComponent> uiComponents;

如果我添加

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

到类public abstract class UIComponent我得到了我想要的JSON格式,即类似

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

我可以在我的Android客户端上进行处理。 我还注意到, @type : ClassName属性被添加到JSON-Output中。 是否不是为了给解组员一个提示而添加的,使用什么类?

使用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]

据我了解,我需要将UIContainer.class修改为

@XmlElementRef
private List<UIComponent> uiComponents;

因此,解组员知道要在这里反序列化什么。

但是,我的问题是,使用@XmlElementRef ,JSON变为:

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

代替

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

有什么办法可以做到这两者?

非常感谢你!

PS:对不起,我的英语不好-我希望阅读/理解不是太难

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

暂无
暂无

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

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