java - Return Map from CXF JAX-RS with name/value formatting -
we want write api looks this:
@xmlrootelement(name = "mydata") @xmlaccessortype(xmlaccesstype.field) public static class mydata { public string name; public map<string,string> data; } @get @path("/mydata") public mydata getmydata() { mydata ret = new mydata(); ret.name = "map data"; ret.data = new hashmap<>(); ret.data.put("a1", "b1"); ret.data.put("a2", "b2"); return ret; }
but here's sticking point cannot seem around: want return json structure this:
{ "mydata":{ "name":"map data", "data":{ "a1": "b1", "a2": "b2" } } }
and can't figure out how beyond like
{ "mydata":{ "name":"map data", "data":{ "entry":[ { "key":"a1", "value":"b1" }, { "key":"a2", "value":"b2" } ] } } }
any idea how might this? i'm pretty sure can done, because once saw demo of it. we're using tomcat, java 7, cxf 2.7.3, , jackson 2.1.2. 2 points:
- note doesn't have contain map necessarily: need marsall bunch of key/values keys not known in advance.
- we have go both directions - need implement put/post syntax in representation.
if want return json (do not support xml), remove jaxb annotations mydata
, pretty structure, without wrapping "mydata" element (which seems redundant me). e.g use core jackson annotations, @jsonignore
(instead of jaxb's). if want add root wrapping element, set wrap_root_value
true.
Comments
Post a Comment