RestEasy: org.codehaus.jackson.map.JsonMappingException: 无法将 START_OBJECT token 转换为 java.util.ArrayList 实例。

3
我有一个REST端点,返回List<VariablePresentation>。我正在尝试测试这个REST端点。
    @Test
    public void testGetAllVariablesWithoutQueryParamPass() throws Exception {
        final ClientRequest clientCreateRequest = new ClientRequest("http://localhost:9090/variables");
        final MultivaluedMap<String, String> formParameters = clientCreateRequest.getFormParameters();
        final String name = "testGetAllVariablesWithoutQueryParamPass";
        formParameters.putSingle("name", name);
        formParameters.putSingle("type", "String");
        formParameters.putSingle("units", "units");
        formParameters.putSingle("description", "description");
        formParameters.putSingle("core", "true");

        final GenericType<List<VariablePresentation>> typeToken = new GenericType<List<VariablePresentation>>() {
        };
        final ClientResponse<List<VariablePresentation>> clientCreateResponse = clientCreateRequest.post(typeToken);
        assertEquals(201, clientCreateResponse.getStatus());
        final List<VariablePresentation> variables = clientCreateResponse.getEntity();
        assertNotNull(variables);
        assertEquals(1, variables.size());

    }

这个测试失败了,并显示以下错误信息:


org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token(..)

我该如何解决这个问题?
1个回答

6
这看起来像是一个Jackson错误,它期望解析一个数组(应该以'['开头),但遇到了一个对象的开始标记('{')。从您的代码中可以看出,我猜它正在尝试将JSON反序列化为您的List,但却得到了对象的JSON。
您的REST端点返回的JSON是什么样子的?它应该长这样:
[
    {
        // JSON for VariablePresentation value 0
        "field0": <some-value>
        <etc...>
    },
    <etc...>
]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接