RestEasy:org.codehaus.jackson.map.JsonMappingException:无法从START_OBJECT标记(..)中反序列化java.util.ArrayList的实例 [英] RestEasy : org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token(..)

查看:265
本文介绍了RestEasy:org.codehaus.jackson.map.JsonMappingException:无法从START_OBJECT标记(..)中反序列化java.util.ArrayList的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个休息端点,它返回 List< VariablePresentation> 。我试图测试这个休息端点为

I have a rest endpoint which returns List<VariablePresentation>. I am trying to test this rest endpoint as

    @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反序列化到你的List中,但是它获得了一个对象的JSON。

That looks like a Jackson error, where it's expecting to parse an array (which would begin with a '[') but it's encountering the beginning token for an object ('{'). From looking at your code, Im guessing it's trying deserialise JSON into your List but it's getting the JSON for an object.

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

What does the JSON your REST endpoint returns look like? It ought to look like this

[
    {
        // JSON for VariablePresentation value 0
        "field0": <some-value>
        <etc...>
    },
    <etc...>
]

这篇关于RestEasy:org.codehaus.jackson.map.JsonMappingException:无法从START_OBJECT标记(..)中反序列化java.util.ArrayList的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆