Android 无法从 Spring 3 REST Web 服务中的 START_ARRAY 令牌反序列化对象实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10719729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:45:22  来源:igfitidea点击:

Cannot deserialize instance of object out of START_ARRAY token in Spring 3 REST Webservice

androidspringHymanson

提问by Gabriela Radu

I am making use of this cool thing Spring offers: Spring RESTWebService (Version of spring is 3). If I access the URL from browser I can see the JSON response, but from a Client endpoint (Android application) iIreceive this error message:

我正在利用 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.Hymanson.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.call(AsyncTask.java:252)
   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
   ... 4 more

 Caused by: org.codehaus.Hymanson.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.Hymanson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:198)
   at org.codehaus.Hymanson.map.deser.BeanDeserializer.deserializeUsingCreator(BeanDeserializer.java:565)
   at org.codehaus.Hymanson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:365)
   at org.codehaus.Hymanson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2395)
   at org.codehaus.Hymanson.map.ObjectMapper.readValue(ObjectMapper.java:1655)
   at org.springframework.http.converter.json.MappingHymansonHttpMessageConverter.readInternal(MappingHymansonHttpMessageConverter.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 structure is the same as the one from the server side application.

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

I have tried to request the server like this:

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

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

Or like this:

或者像这样:

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());

But still the same error as above. Can't figure out what I am missing at this point, any idea or hints would be great. Thank you.

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

采纳答案by Ahto Luuri

Solution in a similar situation was to map to Consultant[].classThis applies if you try to deserialize a JSON array of your mapped objects.

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

回答by pawelzieba

You should map it to List<Consultant>rather than to Consultant.class.

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

回答by NathanChristie

This is related to Hymanson and the way you're attempting to deserialize and initialize a container from an array.

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

While my usage context is a bit different, this may help some who get here from searching for Hymanson-specific deserialization errors.

虽然我的使用上下文有点不同,但这可能会帮助一些人通过搜索 Hymanson 特定的反序列化错误到达这里。

I had to do it like this:

我不得不这样做:

List<Consultant> consultants = Arrays.asList(
    mapper.readValue(myJson.toString(), Consultants[].class)
);

回答by Tiina

a file foo.json contains a batch of data like this:

文件 foo.json 包含一批这样的数据:

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

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

The following statement gives you an ArrayList of LinkedHashMap, where A B C D are the keys.

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

List list = mapper.readValue(new File("D:\...\foo.json"), List.class);