Context classes with Collection elements not unmarshalled
Blabber: 0.5.0 JAXB: 2.2.3 (jaxb-api) Lombok: 1.16.2
I have found that registered Context classes with Collections members do not unmarshall from XML correctly. The example code is not that dissimilar from the actual code ...
-- Context Class --
@NoArgsConstructor @Getter @XmlRootElement(name = "example", namespace = "org.example") @XmlAccessType(XmlAccessType.FIELD) public class Example { @XmlElementWrapper(name = "items") @XmlElement(name = "item") protected List<String> items; }
-- XmppSessionConfiguration -- ... .context(new CoreContext(Example.class); ...
-- XmppSession -- ... session.addInboundMessageListener(new MessageListener() { @Override public void handleMessage(MessageEvent e) { System.out.println(e.getMessage().getExtension(Example.class)); } } ...
When the Message.class is unmarshalled from XML the items are not. Having registered the debugger I can see the following with an empty <items> collection:
IN: <message from="x" to="y" type="headline"><example xmlns="org.example"><items></items></example></message>
But, when I comment out the context class and rerun the same test I see in the debugging output:
IN: <message from="x" to="y" type="headline"><example xmlns="org.example"><items><item>apple</item><item>banana</item><item>carrot</item></items></example></message>
I have tried two POM dependencies firstly javax.xml.bind (2.2.3) and then com.fasterxml.jackson.core (2.5.1) with the same results. Given the fact that Babbler uses JAXB at its core I would have thought this would had worked?
Comments (6)
-
reporter -
repo owner It could possibly be that you have to set the namespace more aggressively, e.g. in more than just the root annotation, e.g.: @XmlElementWrapper(name = "items", namespace = "org.example")
Alternatively you could also try to add a package-info.java and set the namespace there (many examples are in the source code).
-
reporter Thanks Christian for your suggestions. Unfortunately neither worked. I tried removing the namespace to see if it was being too aggressive. Whilst the <items> are displayed in the debugger OUT the context class is no longer Example.class but "class com.sun.org.apache.xerces.internal.dom.ElementNSImpl" instead. This means that when I try and get the instance of Example it is NULL.
-
repo owner Is it still in the CoreContext?
-
reporter Hello Christian. Yes, it is in the CoreContext. I revisited your suggestion of using package-info and managed to get it working. Just needed to add elementFormDefault attribute of QUALIFIED.
@XmlAccessorType(XmlAccessType.FIELD) @XmlSchema(namespace = "org.example", elementFormDefault = XmlNsForm.QUALIFIED) package org.example.messages;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlNsForm; import javax.xml.bind.annotation.XmlSchema;
Many thanks for your support :-)
-
repo owner - changed status to closed
- Log in to comment
Forget to mention that when I try and query the instance of Message class the items is NULL.