Hi Kelly,

you can annotate the the map with a type adapter  like this:

@XmlJavaTypeAdapter(FieldsMapAdaptor.class)
private HashMap<String, String> fields = new HashMap<String, String>();

Here is some sample code for the adapter, but beware that this also changes
the structure of your JSON output (is there a way to use a type adapter
"by-output-type"?)

public static class FieldsMapAdaptor extends XmlAdapter<MyMap, Map<String,
String>> {

        @Override
        public MyMap marshal(Map<String, String> v) throws Exception {
            MyMap myMap = new MyMap();
            List<Field> fieldList = myMap.getField();
            for (Map.Entry<String, String> e : v.entrySet()) {
                fieldList.add(new Field(e.getKey(), e.getValue()));
            }
            return myMap;
        }

        @Override
        public Map<String, String> unmarshal(MyMap v) throws Exception {
            Map<String, String> map = new HashMap<String, String>();
            for (Field e : v.getField()) {
                map.put(e.getKey(), e.getValue());
            }
            return map;
        }


    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(name = "fields")
    public static class MyMap {
        @XmlElement(name = "field", required = true)
        private final List<Field> field = new ArrayList<Field>();

        public List<Field> getField() {
            return this.field;
        }
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(name = "field")
    public static class Field {

        @XmlAttribute(name = "key", required = true)
        private final String key;
        @XmlAttribute(name = "value", required = true)
        private final String value;

        public Field(String key, String value) {
            this.key = key;
            this.value = value;
        }

        public Field() {
            this.key = null;
            this.value = null;
        }

        public String getKey() {
            return key;
        }

        public String getValue() {
            return value;
        }
    }


Best regards, Michael

On Thu, Jun 23, 2011 at 23:14, kldavis4 <[email protected]> wrote:

> I am using Wink 1.1.2. I have a resource like the following:
>
> @Path("/myresource")
> public class MyResource {
>    @GET
>    @Produces(MediaType.APPLICATION_XML)
>    public Map&lt;String,Foo&gt; getResults() {
>        Map&lt;String,Foo&gt; results = new HashMap&lt;String,Foo&gt;();
>        .... //populate results
>        return results;
>    }
> }
>
>
> This doesn't work, I get an error like 'The system could not find a
> javax.ws.rs.ext.MessageBodyWriter or a DataSourceProvider class for the
> java.util.HashMap type and application/xml;q=0.9 mediaType'
>
> Class Foo has JAXB annotations, so if I just return a single Foo instance
> then it works fine.
>
> RESTEasy appears to support the Map type
> (
> http://docs.redhat.com/docs/en-US/JBoss_Enterprise_Web_Platform/5/html/RESTEasy_Reference_Guide/JAXB_Map.html
> )
> so I am wondering if Wink has some kind of similar support.
>
> Thanks, Kelly
>
> --
> View this message in context:
> http://apache-wink-users.3471013.n2.nabble.com/Support-for-Map-type-tp6510043p6510043.html
> Sent from the Apache Wink Users mailing list archive at Nabble.com.
>

Reply via email to