We want to write an API that looks something like 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 the sticking point we cannot seem to get around: we want this to return a JSON structure something like this:
{
"MyData":{
"name":"map data",
"data":{
"a1": "b1",
"a2": "b2"
}
}
}
and we can't figure out how to get beyond something like
{
"MyData":{
"name":"map data",
"data":{
"entry":[
{
"key":"a1",
"value":"b1"
},
{
"key":"a2",
"value":"b2"
}
]
}
}
}
Any idea how we might do this? I'm pretty sure this can be done, because I once saw a demo of it. We're using tomcat, Java 7, CXF 2.7.3, and Jackson 2.1.2. Two points:
- Note that it doesn't have to contain a Map necessarily: all we need is to marsall a bunch of key/values where the keys are not known in advance.
- We have to go both directions - we need to implement PUT/POST as well as GET with this syntax in the representation.