users@jersey.java.net

[Jersey] _at_XmlJavaTypeAdapter doesn't seem to work with Jersey

From: Srinivas Naresh Bhimisetty <shri.naresh_at_gmail.com>
Date: Fri, 16 Nov 2012 20:04:34 +0530

Hello people,

   I have been struggling with the following problem for the last few hours:

Problem Statement:
-----------------------------
    Send a Java Map as a JSONObject with the keys being the map keys and
values being the map values, i.e., lets say, if I have the following map:

                 *Map<String, String> deleteme = new HashMap<String,
String>();*
* deleteme.put("ab","32");*
* deleteme.put("fd","d");*
*
*
  I would like to send this object as : *{deleteme:{"ab":"32", "fd":"d"}}*
*
*
I have tried using the XmlAdapter approach and annotating the map property
with @XmlJavaTypeAdapter, but it doesn't help.


The following is my JAXB bean:

*_at_XmlRootElement*
*_at_XmlAccessorType(FIELD)*
*class MyBean {*
*
*
* @XmlJavaTypeAdapter*
* private Map<String, String> deleteme;*
*
*
* .....*
*
*
*}*

The following is the adapter code:

*class MapElements*
*{*
* @XmlElement public String key;*
* @XmlElement public String value;*
*
*
* private MapElements() {} //Required by JAXB*
*
*
* public MapElements(String key, String value)*
* {*
* this.key = key;*
* this.value = value;*
* }*
*}*
*
*
*
*
*public class MapAdapter extends XmlAdapter<MapElements[], Map<String,
String>> {*
* public MapElements[] marshal(Map<String, String> arg0) throws
Exception {*
* MapElements[] mapElements = new MapElements[arg0.size()];*
* int i = 0;*
* for (Map.Entry<String, String> entry : arg0.entrySet())*
* mapElements[i++] = new MapElements(entry.getKey(),
entry.getValue());*
*
*
* return mapElements;*
* }*
*
*
* public Map<String, String> unmarshal(MapElements[] arg0) throws
Exception {*
* Map<String, String> r = new HashMap<String, String>();*
* for (MapElements mapelement : arg0)*
* r.put(mapelement.key, mapelement.value);*
* return r;*
* }*
*}*
*
*
This is the JSON that's getting generated from the client:

*{deleteme:{"item":[{"key":"ab","value":"32"},{"key":"fd","value":"d"}]}}*
*
*
Could someone please help me solve this problem?

(I'm using Jersey v1.14)

Thanks,
Naresh
*
*