users@jersey.java.net

Re: [Jersey] HashMap manipulation to XML

From: Pavel Bucek <Pavel.Bucek_at_Sun.COM>
Date: Wed, 02 Jun 2010 14:06:43 +0200

On 6/2/10 12:21 PM, Bruno Cardoso wrote:
> Hi Pavel,
>
> I tried using @XmlAnyElement annotation but I get an error:
>
> [javax.xml.bind.JAXBException: class java.util.HashMap nor any of its super class is known to this context.]
>

You can't use XmlAnyElement with HashMap<String, Object>, it needs to
use collection (or single field) of orf.w3c.dom.Element or JAXBElement,
see javadoc [1]. And I can't see a way how to achieve desired output (it
definitely could be a way to go BUT I don't recommend using
XmlAnyElement at all).

>
> I also look at this thread about XMLAdapter:
>
> http://forums.java.net/jive/thread.jspa?threadID=2244
>
> Haven't tried it yet tho.
>
> You think none of the about can help me overcome this situation?
>
> I have this situation where I have some custom fields that I don't
> know about in compile time and I wanted my interface to adapt so it
> would show those custom fields in the same way it shows the other
> attributes.
You can easily have something like

<client>
<id>1</id>
<name>clientName</name>
<customsFields>
<entry key="key1">someValue1</item>
<entry key="key2">someValue2</item>
<entry key="key3">someValue3</item>
</customsFields>
</client>

I will investigate more (actually I;m discussing this with JAXB lead
right now) but I don't think how you could do something like you want
without XmlAnyElement (which could produce "nice" xml, but runtime
processing would need to be more complex).

Pavel


[1]
http://java.sun.com/javaee/5/docs/api/javax/xml/bind/annotation/XmlAnyElement.html


>
> Any ideas?
>
> Thanks
>
> On Wed, Jun 2, 2010 at 9:46 AM, Pavel Bucek <Pavel.Bucek_at_sun.com
> <mailto:Pavel.Bucek_at_sun.com>> wrote:
>
> Hello,
>
> as far as I know, this is not doable with XML processing that JAXB
> provides. You'll need to redesign your Client class if you want to
> achieve that output.
>
> Regards,
> Pavel
>
>
> On 6/1/10 7:28 PM, Charles Overbeck wrote:
>> This seems like more of a JAXB question to me. There is a JAXB
>> user group, users_at_jaxb.dev.net <mailto:users_at_jaxb.dev.net>, that
>> I've found just as helpful as this Jersey one. I'd recommend
>> posting there too.
>>
>> Charles
>>
>>
>> ------------------------------------------------------------------------
>> *From:* Bruno Cardoso <bcardoso.home_at_gmail.com>
>> <mailto:bcardoso.home_at_gmail.com>
>> *To:* users_at_jersey.dev.java.net <mailto:users_at_jersey.dev.java.net>
>> *Sent:* Tue, June 1, 2010 6:34:00 AM
>> *Subject:* Re: [Jersey] HashMap manipulation to XML
>>
>> Anyone ? Is this possible?
>>
>> On Fri, May 28, 2010 at 4:03 PM, Bruno Cardoso
>> <bcardoso.home_at_gmail.com <mailto:bcardoso.home_at_gmail.com>> wrote:
>>
>> Hi,
>>
>> Lets say I have a class with:
>>
>> @XmlRootElement
>> public class Client {
>> private Long id;
>> private String name;
>> private Map<String, Object> customsFields;
>> }
>>
>> This will result in a XML like:
>>
>> <client>
>> <id>1</id>
>> <name>Bruno Cardoso</name>
>> <customFields>
>> <entry>
>> <key>age</key>
>> <value>28</value>
>> </entry>
>> <entry>
>> <key>height</key>
>> <value>178</value>
>> </entry>
>> </customFields>
>> </client>
>>
>> Is it possible to change this behavior somewhere so I can get
>> a XML like this:
>>
>> <client>
>> <id>1</id>
>> <name>Bruno Cardoso</name>
>> <age>28</age>
>> <height>178</height>
>> </client>
>>
>> Thanks
>>
>>
>
>