2013-02-08 74 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...> 
] 
相關問題