RestEasy:org.codehaus.jackson.map.JsonMappingException:无法从 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例(..)

2023-12-02

我有一个返回的休息端点List<VariablePresentation>。我正在尝试测试这个休息端点

    @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(..)

我该如何解决这个问题?


这看起来像 Jackson 错误,它期望解析一个数组(以“[”开头),但遇到了对象的开始标记(“{”)。通过查看您的代码,我猜测它正在尝试将 JSON 反序列化到您的列表中,但它正在获取对象的 JSON。

您的 REST 端点返回的 JSON 是什么样的?它应该看起来像这样

[
    {
        // JSON for VariablePresentation value 0
        "field0": <some-value>
        <etc...>
    },
    <etc...>
]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

RestEasy:org.codehaus.jackson.map.JsonMappingException:无法从 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例(..) 的相关文章

随机推荐