I have read a lot of answers in these forums, as well as other blog posts, but I can't quite seem to connect the pieces together.
So, we start with a basic POJO containing a Map properties. It's well established how to wrap this, but that returns some value. What I'm looking to do is take then name (a.k.a. label) and make it an valid XML 'attribute'. So we would get some value.
I found one example (will link if I can find it again) as follows:
@XmlAnyElement
public List<JAXBElement<String>> getXmlProperties() {
List<JAXBElement<String>> elements = new ArrayList<JAXBElement<String>>();
for (Map.Entry<String, String> property: properties.entrySet())
elements.add(new JAXBElement<String>(new QName(property.getKey()),
String.class, property.getValue()));
return elements;
}
This worked perfectly, but I had this in my Bean/Pojo class, which is shared with a GWT front-end, thus cannot contain references to JAXBElement and QName (source code required).
So, is there a way to get a similar result using something like the XmlAdapter, and the JAXBElement/QName/XmlAnyElement dream team? By the way, I'm using RESTEasy if that factors in at all.
Here is the forum post with the @XmlAnyElement+JAXBElement: Dynamic tag names with JAXB
Depends on the solution listed above, I've came into this
MapAdapter
that addresses both marshaling and unmarshalling process.I've post the full the full post here (in another post), where comments as well as examples are also provided..
I wasn't so far from the answer - after a bit more of experimenting I found the right combo.
Create a wrapper class for the un-mapable return type. Wrapper should contain/return
List<JAXBElement<String>
Annotate wrapper return type with@XmlAnyElement
.Create an XmlAdapter that marshals to the MapWrapper
Annotate your unmappable type with the XmlAdapter
A map like:
Should come out as:
Hope this helps someone else!