In a project I'm currently working on I got an angular exception: Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object?
In my search to find a solution I entered the URL of the web service directly into my browser and surprisingly I did not receive an array as expected.
The web service class:
@Path("/menu")
public class MenuHandler {
@GET
@Path("/cls")
@Produces(MediaType.APPLICATION_JSON)
public List<Clazz> getCLSs() {
Clazz clazz = new Clazz();
Clazz.setFoo("foo");
Clazz.setBar("bar");
ArrayList<Clazz> clazzes = new ArrayList<>();
clazzes.add(clazz);
return clazzes;
}
}
When I enter the url http://localhost:8080/myProject/rest/menu/cls
I would expect to see a JSON array with JSON objects:
[ {"foo": "foo", "bar": "bar"} ]
but instead, I receive an JSON object with a property the JSON object I was expecting without any array:
{
"clazz": {
"foo": "foo",
"bar": "bar"
}
}
So I wondered why there was no array and what would happen when I add another Clazz object. In that case I still get a JSON object but this time one of the parameters is the JSON array that I would expect to have from the start.
{
"clazz": [
{
"foo": "foo",
"bar": "bar"
},
{
"foo": "foo2",
"bar": "bar2"
}
]
}
Can somebody explain me why this behavior is happening and where my thinking went wrong?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…