2013-02-08 41 views
3

我有一个剩余端点,它返回List<VariablePresentation>。我想测试这个休息端点RestEasy:org.codehaus.jackson.map.JsonMappingException:无法将java.util.ArrayList的实例反序列化为START_OBJECT标记(..)

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

我怎样才能解决这个问题?

回答

6

这看起来像一个杰克逊错误,它期望解析一个数组(它将以'['开头,但遇到对象('{')的开始标记)。从查看你的代码,我猜它试图将JSON反序列化到你的List中,但是它得到了一个对象的JSON。

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

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