简体   繁体   中英

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).

I used the Composite-Design-Pattern to create a UIContainer, UIBasicElements, UIComplexElements, and so on...

My Problem is: The UIContainer contains a 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

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

which i can process on my Android-Client. I also noticed, that a @type : ClassName property is added to the 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:

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

@XmlElementRef
private List<UIComponent> uiComponents;

so the unmarshaller knows what to deserialize here.

However, my Problem is, with @XmlElementRef the JSON becomes this:

{ ..., 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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