需要一个对象数组,但在一个对象中得到一个对象

2022-01-21 00:00:00 json java tomcat8 jersey

在我目前正在处理的一个项目中,我遇到了一个角度异常:Error: [$resource:badcfg] 资源配置错误.包含数组但得到对象的预期响应?

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?

在寻找解决方案的过程中,我直接在浏览器中输入了 Web 服务的 URL,但令人惊讶的是,我没有收到预期的数组.

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.

网络服务类:

@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;
    }
}

当我输入 url http://localhost:8080/myProject/rest/menu/cls 我希望看到一个带有 JSON 对象的 JSON 数组:

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"} ]

但相反,我收到一个 JSON 对象,该对象具有我期望的 JSON 对象的属性,但没有任何数组:

but instead, I receive an JSON object with a property the JSON object I was expecting without any array:

{
  "clazz": {
    "foo": "foo",
    "bar": "bar"
  }
}

所以我想知道为什么没有数组以及当我添加另一个 Clazz 对象时会发生什么.在这种情况下,我仍然得到一个 JSON 对象,但这次参数之一是我希望从一开始就拥有的 JSON 数组.

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?

推荐答案

所以我发现了问题.我能够重现它.看来您只是忘记启用 POJOMappingFeature

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>

这使用 Jackson 来反序列化/序列化.如果未设置,它将使用 Jersey 的内部 JSON 提供程序来反序列化/序列化.我不确定如何配置默认提供程序,但我认为还是使用 Jackson 更安全.

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.

除此之外,在我上面的评论中,我说要测试是否使用了 Jackson.轻松完成此操作的一种方法是仅创建一个 ContextResolver 和一个 s.o.p.在 getContext 方法中.当需要映射器时调用此方法.因此,如果正在使用 Jackson,则应该打印 s.o.p

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;
    }  
}

请注意,这也是使用 Jackson 序列化/反序列化进行任何配置的一种方式(只需配置映射器).

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

相关文章