Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

java - Expected an array of objects but got an object in an object

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So I spotted the problem. I was able to reproduce it. It seems you just forgot the enable the POJOMappingFeature

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

This uses Jackson to deserialize/serialize. If this is not set, it uses Jersey's internal JSON provider(s) to deserialize/serialize. I am not sure sure how to configure the default providers, but I think it's safer to just go with Jackson anyway.


Aside, in my comments above I was saying to test if Jackson is being used. One way to easily accomplish this is to just create a ContextResolver and and a s.o.p. inside the getContext method. This method is called when the mapper is needed. So if Jackson is being used, the s.o.p should print

@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {

    private final ObjectMapper defaultMapper;

    public ObjectMapperContextResolver() {
        defaultMapper = new ObjectMapper();
    }

    @Override
    public ObjectMapper getContext(Class<?> type) {
        System.out.println("--- getContext ---");
        return defaultMapper;
    }  
}

Note that this is also a way to make any configurations with the Jackson serization/deserialization (just configure the mapper).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...