Search code examples
springjerseyjsonpjacksonresty-gwt

Spring-Jersey-Jackson-JSON: Array element wrapped by index


I'm writing a restful service using Jersey and Jackson for de/serialization. I'm using Spring to for dependency injection, I'm also using com.sun.jersey.spi.spring.container.servlet.SpringServlet (not using mvc). I'm using RestyGWT on the client side. I'm returning an array of objects from my service, my client is complaining that it is not a valid JSON document. Here is what the service is returning:

   {
    "0": {
        "type": "AQUISITION_DT",
        "value": "2013-2-1",
        "stats": {
            "total": 91,
            "used": 4
        }
    },
    "1": {
        "type": "AQUISITION_DT",
        "value": "2013-1-1",
        "stats": {
            "total": 24,
            "used": 13
        }
    }
}

I'm not sure, but I think the problem is that each element is wrapped by its index. Is there a way I can instruct Jersey or Jackson to unwrap the array elements? Please let me know if I need provide more info.

In the code I'm sending the result back as a JSONWithPadding Object like so: return new JSONWithPadding(array, callback);

btw, I've already configured jersey in my web.xml to use POJO Mapping:

<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>

UPDATE I did a test with curl from the terminal and the string that is returned looks correct (I've removed the callback enclosure):

 {
    {
        "type": "AQUISITION_DT",
        "value": "2013-2-1",
        "stats": {
            "total": 91,
            "used": 4
        }
    },
    {
        "type": "AQUISITION_DT",
        "value": "2013-1-1",
        "stats": {
            "total": 24,
            "used": 13
        }
    }
}

The string that I originally posted is being reported by restyGWT. Sorry for the confusing post, I'm not sure why restyGwt is complaining...

Thanks!


Solution

  • The only way I was able to get around this problem was to wrap the result in an object. Originally I was returning an array (MyObject[]). Now I'm returning MyObjectListing:

    public class MyObjectListing {
       MyObject[] objects;
       ...
       //getters/setters, etc.
    }
    

    returning MyObjectListing solves the problem...I don't understand why.