23

我正在使用 Spring 提供的这个很酷的东西:Spring RESTWebService(Spring 的版本是 3)。如果我从浏览器访问 URL,我可以看到 JSON 响应,但从客户端端点(Android 应用程序)我收到此错误消息:

Caused by: org.springframework.web.client.ResourceAccessException: 
    I/O error: Can not deserialize instance of MyObject out of START_ARRAY token
  at [Source: org.apache.http.conn.EofSensorInputStream@4076e940; line: 1, 
    column: 1]; nested exception is org.codehaus.jackson.map.JsonMappingException: 
    Can not deserialize instance of MyObject  out of START_ARRAY token
  at [Source: org.apache.http.conn.EofSensorInputStream@4076e940; line: 1, column: 1]
   at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:466)
   at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:414)
   at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:234)
   at com.be.android.locateconsultants.resources.AsyncTaskRESTServiceCaller.doInBackground(AsyncTaskRESTServiceCaller.java:43)
   at com.be.android.locateconsultants.resources.AsyncTaskRESTServiceCaller.doInBackground(AsyncTaskRESTServiceCaller.java:1)
   at android.os.AsyncTask$2.call(AsyncTask.java:252)
   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
   ... 4 more

 Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize 
    instance of MyObject  out of START_ARRAY token
  at [Source: org.apache.http.conn.EofSensorInputStream@4076e940; line: 1, column: 1]
   at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:198)
   at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeUsingCreator(BeanDeserializer.java:565)
   at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:365)
   at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2395)
   at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1655)
   at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:135)
   at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)
   at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:74)
   at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:632)
   at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:618)
   at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:459)
   ... 10 more

MyObject 结构与来自服务器端应用程序的结构相同。

我试图像这样请求服务器:

final String url = ".....";
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Consultant> responseEntity = restTemplate.getForEntity(
            url, Consultant.class);

或者像这样:

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(headers);
ResponseEntity<MyObject> response = restTemplate
            .exchange("....",HttpMethod.GET, entity, MyObject.class);
System.out.println("RESPONSE: " + response.getBody());

但还是和上面一样的错误。无法弄清楚我在这一点上缺少什么,任何想法或提示都会很棒。谢谢你。

4

4 回答 4

23

您应该将其映射到List<Consultant>而不是映射到Consultant.class.

于 2012-05-23T15:08:03.987 回答
18

Consultant[].class 如果您尝试反序列化映射对象的 JSON 数组,则类似情况下的解决方案是映射到。

于 2015-02-11T08:27:07.840 回答
9

这与 Jackson 以及您尝试从数组中反序列化和初始化容器的方式有关。

虽然我的使用上下文有点不同,但这可能有助于一些从搜索 Jackson 特定的反序列化错误到这里的人。

我不得不这样做:

List<Consultant> consultants = Arrays.asList(
    mapper.readValue(myJson.toString(), Consultants[].class)
);
于 2015-08-14T15:13:30.767 回答
0

文件 foo.json 包含一批数据,如下所示:

[{"A":"TYMH","B":"datab","C":"DLJQ","D":"datad"}, {"A":"TYMH","B":"datab ","C":"DLJQ","D":"datad"}]

以下语句为您提供 LinkedHashMap 的 ArrayList,其中 ABCD 是键。

List list = mapper.readValue(new File("D:\\...\\foo.json"), List.class);
于 2018-01-09T01:21:38.840 回答