当前位置: 首页 > 知识库问答 >
问题:

在Spring 3 REST Webservice中,无法反序列化START_ARRAY令牌之外的对象实例

齐起运
2023-03-14

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

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

共有1个答案

韩楷
2023-03-14

您应该将其映射到列表 而不是consultant.class

 类似资料: