期望一个对象数组但在对象中得到一个对象
在我正在处理的项目中,我遇到了一个角度异常:错误:[$ 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;
}
}
当我输入网址 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提供程序进行反序列化/序列化。我不确定如何配置默认提供商,但我认为最好还是跟杰克逊一起去。
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.
另外,在我上面的评论中,我说要测试杰克逊是否被使用。一种轻松完成此任务的方法是只创建一个 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 serization / deserialization(只需配置映射器)。
Note that this is also a way to make any configurations with the Jackson serization/deserialization (just configure the mapper).