Domanda

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!

È stato utile?

Soluzione 2

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.

Altri suggerimenti

If you want to return an array of objects, the json format should be like below

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

You can easily return an array in your resource method like below

@Path("/test")
@GET        
@Produces(MediaType.APPLICATION_JSON)
public Test[] haha(){
    Test[] arr = new Test[3]; 
    arr[0] = new Test();
    arr[1] = new Test();
    arr[2] = new Test();
    return arr;
}

Where Test class is like below

public class Test {
    public String type="AQUISITION_DT"; 
    public String value = "2013-2-1";
    public Stats stats = new Stats();
}

public class Stats {
    public int total = 10;
    public int used = 13;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top